【问题标题】:Unity Character ControllerTrying to accomplish smooth movement and jumpingUnity Character Controller 试图完成流畅的移动和跳跃
【发布时间】:2019-06-08 12:40:47
【问题描述】:

当玩家落地时,相机会抖动,而当他跳跃时,他会以一种不顺畅的方式被传送起来

public class PlayerController : MonoBehaviour {

    CharacterController controller;
    Vector3 motion = Vector3.zero;

    #region Movement Variables
    public float walkingSpeed = 4f;
    public float runningSpeed = 6f;
    public float jumpSpeed = 5f;
    public float gravity = 9.8f;
    #endregion Movement Variables

    void Start () {
        controller = GetComponent<CharacterController>();
    }

    void Update () {
        motion = Vector3.zero;

        motion.x = Input.GetAxis("Horizontal");
        if (controller.isGrounded && Input.GetAxis("Vertical")>0) motion.y += jumpSpeed;
        if (Input.GetKey(KeyCode.LeftShift))
        {
            motion.x *= runningSpeed;
        }
        else
        {
            motion.x *= walkingSpeed;
        }
        motion.y -= gravity;
    }

    void FixedUpdate()
    {
        controller.Move(motion * Time.deltaTime);
    }
}

我正在尝试创建流畅的运动,目前它根本不可靠。感谢您的帮助

【问题讨论】:

    标签: visual-studio unity3d game-physics platform


    【解决方案1】:

    controller.Move(motion * Time.deltaTime);从FixedUpdate中取出,放在Update方法的最后。

    如果您在相机上也有一些脚本,例如,跟随相机的某些实现,您在更新循环中使用该相机的变换组件,则应改为在 LateUpdate 中。

    看到这个https://learn.unity.com/tutorial/update-and-fixedupdate#5c8a4242edbc2a001f47cd63https://docs.unity3d.com/ScriptReference/MonoBehaviour.LateUpdate.html

    这个具有角色控制器组件的 GameObject 玩家控制器的实现也可能对您有所帮助。

    void Update()
        {
            if (isDead) return;
    
            isWalking = Input.GetButton("Walk");
            isGrounded = characterController.isGrounded;
    
            if (isGrounded)
            {
                // Move
                float verticalAxis = Input.GetAxis("Vertical");
    
                moveDirection = new Vector3(0, 0, verticalAxis);
                moveDirection = transform.TransformDirection(moveDirection);
    
                if (verticalAxis > 0 && isWalking)
                {
                    moveDirection *= walkingSpeed;
                }
                else if (verticalAxis > 0)
                {
                    moveDirection *= runningSpeed;
                }
    
                // Jump
                if (Input.GetButtonDown("Jump"))
                {
                    moveDirection.y = jumpSpeed;
                    //moveDirection.z = jumpDistance; in case some forward boost is required
                    moveDirection = transform.TransformDirection(moveDirection);
                }
            }
            // Gravity
            moveDirection.y -= gravity * Time.deltaTime;
            characterController.Move(moveDirection * Time.deltaTime);
    
            // Rotation
            float horizontalAxis = Input.GetAxis("Horizontal");
            transform.Rotate(0, horizontalAxis, 0);
            transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.FromToRotation(transform.up, GetHitNormal()) * transform.rotation, yRotationSpeed * Time.deltaTime);
        }
    
    // To keep local up aligned with global up on a slope ground
        private Vector3 GetHitNormal()
        {
            RaycastHit hit;
            if (Physics.Raycast(transform.position, Vector3.down, out hit))
                return hit.normal;
            else
                return Vector3.zero;
        }
    

    【讨论】:

      猜你喜欢
      • 2023-01-23
      • 1970-01-01
      • 1970-01-01
      • 2014-01-16
      • 1970-01-01
      • 1970-01-01
      • 2021-12-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多