Unity for Android-对象不跟随触摸输入

本文关键字:触摸 输入 不跟随 对象 for Android- Unity | 更新日期: 2023-09-27 17:59:45

我刚刚开始学习Unity(使用C#),我正在尝试创建脚本,该脚本将使脚本所附的对象跟随我的触摸输入(当我移动手指时,基本上跟随它)。

我想做的是:

编辑:这是我现在的更新版本。

using UnityEngine;
using System.Collections;
using System;
public class PlayerMovement : MonoBehaviour {

    private Vector3 fingerEnd;
    void Start () {
    }
    void Update () {
        foreach(Touch touch in Input.touches)
        {
            if (touch.phase == TouchPhase.Began)
            {
                fingerEnd = touch.position;
            }
            if (touch.phase == TouchPhase.Moved && (Math.Sqrt(Math.Pow(fingerEnd.x,2) + Math.Pow(fingerEnd.y,2)) - Math.Sqrt(Math.Pow(transform.position.x,2) + Math.Pow(transform.position.y,2))<100))
            {
                fingerEnd = touch.position;
                Vector3 worldCoordinates = Camera.main.ScreenToWorldPoint(fingerEnd);
                transform.Translate(worldCoordinates);
            }
        }
    }
}

我也尝试过使用Vector2.Lerp,但我的对象仍然没有移动。这可能是一个非常基本的错误,但我不认为什么在这里不起作用。

我还试着改变

fingerEnd = touch.position;
Vector3 worldCoordinates = Camera.main.ScreenToWorldPoint(fingerEnd);
transform.Translate(worldCoordinates);

fingerEnd = touch.position;
transform.Translate(fingerEnd);

fingerEnd = touch.position;
Vector3 worldCoordinates = Camera.main.ScreenToWorldPoint(fingerEnd);
transform.position = worldCoordinates;

fingerEnd = touch.position;
transform.position = fingerEnd ;

没有效果。

编辑:好的,我已经解决了。这是我的错误。在IF子句中,我使用了touch.position(fingerEnd)值,之前我知道这是屏幕上像素的位置。我把它比作单位坐标中的transform.position。当我把这两个条件从IF.中分离出来时,它就起了作用

代码的最终版本:

using UnityEngine;
using System.Collections;
using System;
public class PlayerMovement : MonoBehaviour {
    Vector3 worldCoordinates;
    void Start () {
    }
    void Update () {
        foreach(Touch touch in Input.touches)
        {
            if (touch.phase == TouchPhase.Began)
            {
                Debug.Log("Touch: " + touch.position);
            }
            if (touch.phase == TouchPhase.Moved) 
            {
                worldCoordinates = Camera.main.ScreenToWorldPoint(touch.position);
                if ((Math.Sqrt(Math.Pow(worldCoordinates.x,2) + Math.Pow(worldCoordinates.y,2)) - Math.Sqrt(Math.Pow(transform.position.x,2) + Math.Pow(transform.position.y,2))<0.2))
                {
                    Debug.Log("Touch: " + touch.position);
                    Debug.Log("Transform: " + transform.position);
                    Debug.Log("WorldCoordinates: " + worldCoordinates);
                    //transform.Translate(worldCoordinates);
                    transform.position = new Vector3(worldCoordinates.x,worldCoordinates.y);
                    //transform.Translate(touch.position);
                    Debug.Log("Transform 2: " + transform.position);
                }
            }
        }
    }
}

对象现在跟随我的手指。这有点慢(除非我停下来,否则一直落后),但这是另一个需要解决的问题。非常感谢你的帮助。

Unity for Android-对象不跟随触摸输入

touch.position以像素坐标表示的触摸位置。

因此,首先,您需要将位置转换为世界(统一世界)坐标。

你应该使用Camera.ScreenToWorldPoint来编写代码(也许是主摄像头)。

例如:

Vector3 worldCoordinates = yourCamera.ScreenToWorldPoint(fingerEnd);
transform.Translate(worldCoordinates);

您需要更改

transform.position = fingerEnd;  

transform.Translate(fingerEnd);