如何让玩家不断移动,又能平滑移动到固定位置?

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

我希望玩家不断地向 z 方向移动,但也希望将其与选项相结合,以像在 Vector3 中一样平滑过渡将玩家移动到 3 个固定 x 位置(-1.5、0 或 1.5)之一。飞跃功能。我尝试了几个选项,但都没有用。

有人知道我该怎么做吗?

unity3d 3d
1个回答
0
投票

拆分两者并在Z轴上不断移动但在X轴上插值:

[SerializeField] private float zVelocity = 1f;
[SerializeField] private float smoothTime = 0.3f;

private float xVelocity = 0f;
private float targetX = 0f;

public void SetTargetX(float newTargetX)
{
    targetX = newTargetX;
}

public void SetZVelocity(float newZVelocity)
{
    zVelocity = newZVelocity;
}

private void Update ()
{
    var position = transform.position;
    position.z += zVelocity * Time.deltaTime;
    position.x = Mathf.SmoothDamp(position.x, targetX, ref xVelocity, smoothTime);
    transform.position = position;
}
© www.soinside.com 2019 - 2024. All rights reserved.