0

我的项目是第一人称 Jump&Run,我希望我的玩家通过按下水平或垂直结合 Shift 来冲刺。

我已经创建了一个名为 Sprint 的新输入,带有负按钮“左移”

我的玩家可以正常移动,但他不会 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);

    }
}
4

1 回答 1

1

处理输入的方式很可能存在问题。我的假设是当你按下 shift down 时,GetButtonDown 只为单帧返回 true。请改用 GetKey:

Input.GetKey(KeyCode.LeftShift)

如果这不起作用,请尝试使用这两种方法:

Input.GetKeyDown("left shift")

Input.GetKeyDown(KeyCode.LeftShift)

但是这两个可能与“GetButtonDown”存在相同的问题。

另外,我认为如果您使用英语注释而不是德语,它会帮助人们更好地理解您的代码。我能够阅读它,但很可能不会。不过别担心,这也发生在我身上!

于 2021-01-18T12:27:51.220 回答