如何使用角色控制器正确跳跃?

How to jump properly using character controller?

我正在为我的玩家移动使用角色控制器。我走路和 运行 工作,但我无法跳起来工作。

这是我的代码:

[SerializeField] Transform playerCamera = null;
[SerializeField] float mouseSensitivity = 3.5f;
[SerializeField] float walkSpeed = 10.0f;
[SerializeField] float RunSpeed = 12.0f;
[SerializeField] float gravity = 9.81f;
[SerializeField] bool lockCursor = true;
[SerializeField] [Range(0.0f, 0.5f)] float moveSmoothTime = 0.3f;
[SerializeField] [Range(0.0f, 0.5f)] float mouseSmoothTime = 0.03f;
public float jumpHeight = 3f;

Vector3 velocity;
public float verticalVelocity;
float cameraPitch = 0.0f;
float VelocityY = 0.0f;
CharacterController controller = null;

Vector2 currentDir = Vector2.zero;
Vector2 currentDirVelocity = Vector2.zero;

Vector2 currentMouseDelta = Vector2.zero;
Vector2 currentMouseDeltaVelocity = Vector2.zero;

void Start()
{
    controller = GetComponent<CharacterController>();
    if (lockCursor)
    {
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }
}
void Update()
{
    UpdateMouseLook();
    UpdateMovement();
}
void UpdateMouseLook()
{
    Vector2 targetMouseDelta = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));

    currentMouseDelta = Vector2.SmoothDamp(currentMouseDelta, targetMouseDelta, ref currentMouseDeltaVelocity, mouseSmoothTime);

    cameraPitch -= currentMouseDelta.y * mouseSensitivity;

    cameraPitch = Mathf.Clamp(cameraPitch, -90.0f, 90.0f);

    playerCamera.localEulerAngles = Vector3.right * cameraPitch;

    transform.Rotate(Vector3.up * currentMouseDelta.x * mouseSensitivity);
}
void UpdateMovement()
{
    Vector2 targetDir = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
    targetDir.Normalize();

    currentDir = Vector2.SmoothDamp(currentDir, targetDir, ref currentDirVelocity, moveSmoothTime);

    if (controller.isGrounded)
        VelocityY = 0.0f;

    VelocityY += gravity * Time.deltaTime;

    velocity = (transform.forward * currentDir.y + transform.right * currentDir.x) * walkSpeed + Vector3.up * VelocityY;

    controller.Move(velocity * Time.deltaTime);

    if ((Input.GetKey("left shift") || Input.GetKey("right shift")) && controller.isGrounded && !Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.DownArrow))
    {
        velocity = (transform.forward * currentDir.y + transform.right * currentDir.x) * RunSpeed + Vector3.up * VelocityY;

        controller.Move(velocity * Time.deltaTime);
    }

    if (Input.GetKeyDown(KeyCode.J) && controller.isGrounded)
    {

        Debug.Log("Now Jumping!!");
        VelocityY = Mathf.Sqrt(jumpHeight * -2f * gravity);
       
    }
}

注意:我没有在我的角色上使用 rigidbody

跳转不是每次都有效,在某些点击上,它会跳转,而在大多数点击上什么也没有发生,甚至 Debug.Log("Now Jumping!!"); 每次我点击 J 时都不会打印出来。我该如何解决这个问题?

我解决了这个问题。您别无选择,只能将物理添加到角色控制器。这是一个简单的代码,具有相同的功能,但你应该协调移动和跳跃。

public class Player : MonoBehaviour
{
    private Vector3 playerVelocity;
    private CharacterController _controller;

    public float jumpPower = 1f; // 1 is ok for -9.8 gravity
    void Start()
    {
        _controller = GetComponent<CharacterController>();
    }
    void Update()
    {
        playerVelocity += Physics.gravity * Time.deltaTime;

        _controller.Move(playerVelocity);

        if (_controller.isGrounded)
        {
            playerVelocity.y = Input.GetKeyDown(KeyCode.Space) ? jumpPower : 0;
        }
    }
}