我应该更改什么以在 Unity 中获得更流畅的(点击并按住)控制?

What should I change to get smoother (tap and hold) control in Unity?

我一直在尝试用我的代码在 Y 轴上平滑移动(例如在 iOS 游戏 Space Frontier 中),但是它抖动,绝对不好,不平滑等。我应该如何尝试不同的方法?我想向上移动的播放器有一个 2D 刚体

public class MovementOnTouch : MonoBehaviour


    public float currentVelocity = 0.0f;
    public float accelerationRate = 0.0079f;
    public float decelerationRate = 3.0f;
    public float initialVelocity = 1.0f;
    public float finalVelocity = 2.0f;

    // Update is called once per frame
    void Update()
    {
        for (int i = 0; i < Input.touchCount; i++)
        {
            if (Input.touchCount > 0)
            {
                currentVelocity = currentVelocity + (accelerationRate * Time.fixedDeltaTime);
                transform.Translate(0, currentVelocity,0 ); //
            }
            else
            {

                currentVelocity = currentVelocity - (decelerationRate * Time.fixedDeltaTime);
                if (currentVelocity > 0)
                {
                    transform.Translate(0, currentVelocity,0 ); //
                }
                else
                {
                    transform.Translate(0, 0, 0);
                }
            }
            currentVelocity = Mathf.Clamp(currentVelocity, initialVelocity, finalVelocity);
        }
    }

}

不要使用for (int i = 0; i < Input.touchCount; i++);如果 touchCount 增长,它将导致延迟。

尝试删除它并保持其余代码不变,也许只是更改加速变量:

for (int i = 0; i < Input.touchCount; i++)
{

}