首页 > 解决方案 > 球员跳得不好

问题描述

我可以移动播放器引用此 YouTube 视频。 Unity 中的第一人称动作 - FPS 控制器

但是,当玩家跳跃时,如果行进方向是墙壁,则玩家不能很好地跳跃。 像这样

如果我从远处跳,我可以跳到墙上。

相机脚本:

public float mouseSensitivity;

[SerializeField]
Transform playerBody = default;

float xRotation = 0f;

private void Start()
{ 
    Cursor.lockState = CursorLockMode.Locked;
}

void Update()
{
    float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
    float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;

    xRotation -= mouseY;
    xRotation = Mathf.Clamp(xRotation, -90f, 90f);

    transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
    playerBody.Rotate(Vector3.up * mouseX);
}

运动脚本:

[SerializeField]
CharacterController CharacterController = default;

[SerializeField]
LayerMask groundMask = default;
[SerializeField]
Transform groundCheck = default;
float groundDistance = 0.4f;
bool isGrounded = false;

public float playerSpeed;
public float playerGravity;
public float playerJumpSpeed;

Vector3 velocity;

private 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;

    CharacterController.Move(move * playerSpeed * Time.deltaTime);

    if (Input.GetButtonDown("Jump") && isGrounded)
    {
        velocity.y = Mathf.Sqrt(playerJumpSpeed * -2f * playerGravity);
    }

    velocity.y += playerGravity * Time.deltaTime;

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

请告诉我某人

标签: c#unity3d

解决方案


将 Character 控制器的 Step Offset 设置为 0 可消除振动。它不能再小步走,但现在我可以跳得很好了。


推荐阅读