【问题标题】:Unity for Android - object not following touch inputsUnity for Android - 对象不跟随触摸输入
【发布时间】:2015-06-26 06:49:36
【问题描述】:

我刚刚开始学习 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);
                }
            }
        }
    }
}

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

【问题讨论】:

    标签: c# android unity3d touch


    【解决方案1】:

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

    所以首先需要将位置转换为世界(统一世界)坐标。

    您应该在代码中使用Camera.ScreenToWorldPoint(可能是主摄像头)。

    例如:

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

    【讨论】:

    • 我觉得这是正确的方向,但我的相机有问题。我还尝试设置新参数(私有相机 mainCamera;),并使用“mainCamera = GetComponent()”对其进行初始化。然后我使用你给出的两条线(当然用 mainCamera 而不是 yourCamera)。不过,没有任何效果。我假设,我应该使用从一开始就已经在项目中的那个,而不是新的 Camera 参数,但我不确定如何(尝试使用 gameobject.getobjectwithtag,但它没有用)。
    • 你试过 Camera.main.ScreenToWorldPoint 吗?
    • 我现在已经尝试过了,但无论是否使用 ScreenToWorlPoint,问题似乎都一样。我的对象从屏幕中间开始,但是当我在屏幕顶部(从左上角或右上角)滑动我的精细时它会做出反应。它也不跟随我的触摸,只是在左上角方向快速移动(我立即看不见它)。我显然在坐标上做错了什么。我在横向位置使用手机。我已经在主帖中添加了更新的代码。
    • 如果我理解你的话,也许你直接设置位置而不是翻译。 (this.transform.position=worldCoordinates;这个直接设置位置,请试一试。
    • 好的,解决了。如果有人需要它,我在主帖中解释了它(我怀疑;])。
    【解决方案2】:

    你需要改变

    transform.position = fingerEnd;  
    

    transform.Translate(fingerEnd);
    

    【讨论】:

    • 这部分很好,我的对象开始对某些东西做出反应,但不是按照我想要的方式。在一个特定的移动之后,它会移动到很远的坐标(例如 x = 6000+ 和 y = 4000+)。我认为它与另一个答案中提到的 ScreenToWorldPoint 有关
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多