问题描述
我的项目是第一人称跳跃和奔跑,我希望我的玩家通过按 Horizonzal 或 Vertical 并结合 Shift 来冲刺。
我已经创建了一个名为 Sprint 的新输入,带有负按钮“左移”
我的玩家可以正常移动,但没有冲刺。
非常感谢。
public class PlayerMovement : MonoBehavIoUr
{
public CharacterController controller;
public float speed = 12f;
public float sprint;
public float gravity = -9.81f;
public float jumpHeight = 3f;
public Transform groundCheck;
public float grounddistance = 0.4f;
public LayerMask groundMask;
Vector3 veLocity;
bool isGrounded;
// Update is called once per frame
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position,grounddistance,groundMask);
if(isGrounded && veLocity.y < 0) {
veLocity.y = -2f;
}
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
if (Input.GetButtonDown("Jump") && isGrounded)
{
veLocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
veLocity.y += gravity * Time.deltaTime;
controller.Move(veLocity * Time.deltaTime);
// Noch nicht fertig -> Noch ausstehend
if (Input.GetButtonDown("Horizontal") && Input.GetButtonDown("Sprint") || Input.GetButtonDown("Vertical") && Input.GetButtonDown("Sprint"))
{
controller.Move(move * (speed + sprint) * Time.deltaTime);
}
}
解决方法
输入的处理方式很可能存在问题。 我的假设是 GetButtonDown 仅在您按下 shift 时为单帧返回 true。改用 GetKey:
Input.GetKey(KeyCode.LeftShift)
如果这不起作用,请尝试使用这 2 个:
Input.GetKeyDown("left shift")
Input.GetKeyDown(KeyCode.LeftShift)
但是这两个可能与“GetButtonDown”存在相同的问题。
此外,我认为如果您使用英文注释而不是德文注释,它会帮助人们更好地理解您的代码。我能够阅读它,但很可能不会。不过别担心,这也发生在我身上!