我正在尝试制作触摸屏游戏,但在运动中挣扎

时间:2019-12-01 17:14:42

标签: c# mobile

当前,我正在使用矢量移动角色,但是当角色到达触摸位置时,它会继续向前移动,因为速度必须减慢。我希望角色能够到达手指位置(正在执行此操作),但要立即停止。

有什么建议吗?

我的代码:

private float? pressX;
float currentX;
public float moveSpeed = 1f;
void Update()
    {

        if (Input.GetMouseButtonDown(0))
            pressX = Input.touches[0].position.x;
        else if (Input.GetMouseButtonUp(0))
            pressX = null;
        if (pressX != null)
        {
            float currentX = Input.touches[0].position.x;
            // The finger of initial press is now left of the press position
            if (currentX < pressX)

                Move(-moveSpeed);

            // The finger of initial press is now right of the press position
            else if (currentX > pressX)

                Move(moveSpeed);

            // else is not required as if you manage (somehow)
            // move you finger back to initial X coordinate
            // you should just be staying still
        }


    void Move(float velocity)
    {
        transform.position += Vector3.right * velocity * Time.deltaTime;
    }

0 个答案:

没有答案