unity中的slerp函数可以让人物走奇怪的圈子吗?

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

我尝试通过以下教程来实现统一: https://www.youtube.com/watch?v=AmGSEH7QcDg&t=30084s

1:22:30,老师想让玩家人物旋转到行走方向,添加如下代码:

transform.forward = Vector3.Slerp(transform.forward, moveDir, Time.deltaTime * rotateSpeed);

当我添加它时,我的玩家开始通过按方向键在奇怪的圆圈中行走。

这是我的完整代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;



public class Player : MonoBehaviour
{
  [SerializeField] private float moveSpeed = 7f;
  private void Update() {
        Vector2 inputVector = new Vector2(0, 0);
        if (Input.GetKey(KeyCode.W)) {
            inputVector.y = +1;
        }
        if (Input.GetKey(KeyCode.S)) {
            inputVector.y = -1;
        }
        if (Input.GetKey(KeyCode.A)) {
            inputVector.x = -1;
        }
        if (Input.GetKey(KeyCode.D)) {
            inputVector.x = +1;
        }



        inputVector = inputVector.normalized;

        Vector3 moveDir = new Vector3(inputVector.x, 0f, inputVector.y);
        transform.position += moveDir * moveSpeed * Time.deltaTime;

        //float rotateSpeed = 10f; //1:22:36
        //transform.forward = Vector3.Slerp(transform.forward, moveDir, Time.deltaTime * rotateSpeed);
        float rotateSpeed = 10f;
        transform.forward = Vector3.Slerp(transform.forward, moveDir, Time.deltaTime);
    }
}

感谢您的帮助

c# unity-game-engine visual-studio-code
1个回答
0
投票

PlayerVisual 的旋转中心不正确。 确保PlayerVisual组件的位置为(0,0,0)。

© www.soinside.com 2019 - 2024. All rights reserved.