Android 统一 - 对象不跟随触摸输入
Unity for Android - object not following touch inputs
我刚开始学习 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 进行了比较。当我将这 2 个条件与 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);
}
}
}
}
}
物体现在跟随我的手指。它有点慢(除非我停下来,否则一直留在后面)但这是另一个需要解决的问题。非常感谢您的帮助。
你需要改变
transform.position = fingerEnd;
到
transform.Translate(fingerEnd);
touch.position The position of the touch in pixel coordinates.
所以首先需要将position转换为world(unity world)坐标
你应该在你的代码中使用 Camera.ScreenToWorldPoint(也许是主摄像头)。
例如:
Vector3 worldCoordinates = yourCamera.ScreenToWorldPoint(fingerEnd);
transform.Translate(worldCoordinates);
我刚开始学习 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 进行了比较。当我将这 2 个条件与 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);
}
}
}
}
}
物体现在跟随我的手指。它有点慢(除非我停下来,否则一直留在后面)但这是另一个需要解决的问题。非常感谢您的帮助。
你需要改变
transform.position = fingerEnd;
到
transform.Translate(fingerEnd);
touch.position The position of the touch in pixel coordinates.
所以首先需要将position转换为world(unity world)坐标
你应该在你的代码中使用 Camera.ScreenToWorldPoint(也许是主摄像头)。
例如:
Vector3 worldCoordinates = yourCamera.ScreenToWorldPoint(fingerEnd);
transform.Translate(worldCoordinates);