尝试向后移动时出现 Unity 3D 运动问题

问题描述 投票:0回答:0

我的播放器控制器在向前、向左和向右移动时工作正常。摄像机始终朝向玩家移动/旋转的方向,这很棒。

问题是当我按下并尝试向后移动时。镜头摇晃,就好像玩家试图旋转 180 度但不能。

我怎样才能让向下箭头平滑地向后移动播放器? 这是脚本:

{
    [Header("Movement")]
    [SerializeField] float speed;
    [SerializeField] float rotationSmoothTime;

    [Header("Gravity")]
    [SerializeField] float gravity = 9.8f;
    [SerializeField] float gravityMultiplier = 2;
    [SerializeField] float groundedGravity = -0.5f;
    [SerializeField] float jumpHeight = 3f;
    float velocityY;

    CharacterController controller;
    //Camera cam;

    float currentAngle;
    float currentAngleVelocity;
    public Transform player;


    private void Awake()
    {
        //getting reference for components on the Player
        controller = GetComponent<CharacterController>();
       // cam = Camera.main;
    }

    private void Update()
    {
        HandleMovement();
        HandleGravityAndJump();
    }

    **private void HandleMovement()
    {
        //capturing Input from Player
        Vector3 movement = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")).normalized;
        if (movement.magnitude >= 0.1f)
        {
            //compute rotation
            float targetAngle = Mathf.Atan2(movement.x, movement.z) * Mathf.Rad2Deg + player.eulerAngles.y;
            currentAngle = Mathf.SmoothDampAngle(currentAngle, targetAngle, ref currentAngleVelocity, rotationSmoothTime);
            transform.rotation = Quaternion.Euler(0, currentAngle, 0);
            //move in direction of rotation
            Vector3 rotatedMovement = Quaternion.Euler(0, targetAngle, 0) * Vector3.forward;
            controller.Move(rotatedMovement * speed * Time.deltaTime);
        }
    }**

    void HandleGravityAndJump()
    {
        //apply groundedGravity when the Player is Grounded
        if (controller.isGrounded && velocityY < 0f)
            velocityY = groundedGravity;

        //When Grounded and Jump Button is Pressed, set veloctiyY with the formula below
        if (controller.isGrounded && Input.GetKeyDown(KeyCode.Space))
        {
            velocityY = Mathf.Sqrt(jumpHeight * 2f * gravity);
        }

        //applying gravity when Player is not grounded
        velocityY -= gravity * gravityMultiplier * Time.deltaTime;
        controller.Move(Vector3.up * velocityY * Time.deltaTime);
    }
}
unity3d user-controls game-development unityscript
© www.soinside.com 2019 - 2024. All rights reserved.