我需要帮助添加平滑到我的球员模型旋转C#统一



嘿,我对编码很陌生,我正在尝试使用rigidbody纠正一个简单的玩家移动脚本。我已经能够让我的球员移动并转身面对它的移动方向,我只是不知道如何让它顺利完成。我已经为turnspeed/smooting制作了一个float,但不知道如何将其实现到我的代码中。如果我的代码混乱或错误,我很抱歉,我对此很陌生,希望能得到一些建设性的建议。

我的代码:

public float smoothing = .1f;
public void Update()
{       
movement = new Vector3(Input.GetAxisRaw("Horizontal"), 0f, Input.GetAxisRaw("Vertical")).normalized;
private void FixedUpdate()
{
moveCharacter(movement);
private void moveCharacter(Vector3 direction)
{
// player look direction
Vector3 lookDirection = movement + gameObject.transform.position;
gameObject.transform.LookAt(lookDirection);

// movement
rigidBodyComponant.MovePosition(transform.position + (direction * speed * Time.deltaTime));
}

为了平滑位置变化,我喜欢使用Lerping。例如,这可以用于警告旋转。

Quaternion toRotation = Quaternion.FromToRotation(transform.forward, direction);
transform.rotation = Quaternion.Lerp(transform.rotation, toRotation, speed * Time.deltaTime);

您应该将其放在FixedUpdate()中以获得平滑的结果。你可以通过给它一个特定的值来决定速度。

最新更新