如何使用触摸使角色沿轴移动?

How to make the character move along the axis using touch?

我有以下代码可以工作,但我不知道它是如何工作的。

void Update()
{
#if UNITY_ANDROID

    if(Input.touchCount>0 && Input.GetTouch(0).phase == TouchPhase.Moved )
    {
        Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
        float offset = touchDeltaPosition.x * 40f / 18 * Mathf.Deg2Rad;
        transform.position += new Vector3(0f, 0f, offset);
        transform.position = new Vector3(0f, 0f, Mathf.Clamp(transform.position.z, -7.1f, 7.1f));
    }     
#endif   
}

我需要通过触摸让玩家沿轴移动,例如在游戏 Cube Surfer 中!

我想得到什么(我用的是android模拟器):

我根据以下答案编写的代码在 720x1280 下运行良好,但如果将分辨率设置为 1440x2960,控件会变得过于清晰。我知道为什么会这样,因为 touch.delta 变得太大了。

我的代码:

if (Input.touchCount > 0)
{
    var t = Input.GetTouch(0);

    var delta = t.deltaPosition;

    if (delta != Vector2.zero)
    {
        var offset = transform.position;

        offset.x += delta.x * Sensitivity * Time.deltaTime;

        offset.x = Mathf.Clamp(offset.x, -4f, 4f);

        transform.position = offset;
    }
}

如何解决?

ScreenToWorldPoint 这不适合因为玩家沿着样条曲线移动。

感谢帮助

我会推荐一个更简单易懂的代码

if (Input.touchCount > 0)
    {
        touch = Input.GetTouch(0);
        if (touch.phase == TouchPhase.Moved)
        {
            transform.position = new Vector3(
               transform.position.x + touch.deltaPosition.x * speedModifier,
               transform.position.y,
               transform.position.z);
        }
    }
  • 对于Input.touchCount > 0,您正在检查是否有触摸
  • 使用 touch = Input.GetTouch(0); if (touch.phase == TouchPhase.Moved) 您正在检查触摸是否正在移动或拖动
  • 最后一部分只是我想要的沿轴的移动,在我的例子中只是 x 轴,其中 speedModifier 是移动的速度。

如果你想在你的计算机上尝试它,你需要用光线投射做类似的事情,但是如果你插入你的 phone yo unity 它会像这样工作。

您只需添加 + touch.deltaPosition.x * speedModifier

即可轻松沿其他轴移动它

使用此代码

每一帧我们都会计算您当前触摸的增量移动并将其乘以 speed(在一个时间范围内,这就是乘以 Time.deltaTime 的原因)

    using System.Collections.Generic;
    using UnityEngine;
 
     public class BasicMovement : MonoBehaviour {

     public float speed=5;
 
     void Update () { 
        if(Input.touchCount>0 && Input.GetTouch(0).phase == TouchPhase.Moved )
        {
           Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
           float offset = touchDeltaPosition.x * speed * Time.deltaTime;
           transform.position = new Vector3(0f, 0f, Mathf.Clamp(offset , -5f,5f));
        }

     }
 }
float worldWidth         = 14f;  //serialized, example value
float ratioScreenToWorld = 2.0f; //serialized, example value

float screenWidth        = (float) Screen.width;

if (Input.touchCount > 0)
{
    Touch touch = Input.GetTouch(0);
    if (touch.phase == TouchPhase.Moved)
    {
        float horizontal = touch.deltaPosition.x;
        transform.position +=
            new Vector3(0f, 0f, horizontal * (worldWidth / screenWidth) * ratioScreenToWorld);
        //clamp, or whatever
    }
}

你的问题的根源在于你没有将手指的移动标准化到某个标准。

对于 ratioScreenToWorld = 1f,此代码将手指在屏幕上的偏移量映射到路径上对象的偏移量。路径的宽度由 worldWidth 指定。更改 ratioScreenToWorld 后,它会重新按比例分配该映射,例如用 ratioScreenToWorld = 0.5f 通俗地说,“在整个屏幕上移动手指会使 road/path 上的对象移动路径宽度的一半”。

每一帧我们都会计算您当前触摸的增量移动并将其乘以速度(在一个时间范围内,这就是乘以 Time.deltaTime 的原因)