C# 基于触摸移动精灵位置
我需要从一个触摸输入,然后移动一个精灵到那个位置,这就是我到目前为止所做的C# 基于触摸移动精灵位置,c#,ios,unity3d,C#,Ios,Unity3d,我需要从一个触摸输入,然后移动一个精灵到那个位置,这就是我到目前为止所做的 public class PlayerContoller : MonoBehaviour { public Sprite Hands; public Vector2 HandsLocation; void Start () { } // Update is called once per frame void Update () { if (Inp
public class PlayerContoller : MonoBehaviour {
public Sprite Hands;
public Vector2 HandsLocation;
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.touchCount > 0){
KnifeTouch ();
}
}
void KnifeTouch () {
Input.GetTouch(0).position = HandsLocation
}
}
谢谢我没有使用mono或unity3d,我使用的是XNA,但我认为规则是一样的 您的代码: Input.GetTouch(0).position=HandsLocation 您是否尝试将手持位置设置为设备输入位置? 我想您可以读取设备输入数据并将其用于您的操作 您需要执行反向操作: HandsLocation=Input.GetTouch(0.position) 现在将输入数据存储在控制器中,然后使用它移动精灵:
void Update () {
if (Input.touchCount > 0){
//KnifeTouch ();
HandsLocation = Input.GetTouch(0).position; // store input position
}
// after read position, use it to move sprite
}
输入.GetTouch().position返回像素坐标,将其转换为世界坐标,编写以下代码
Vector3 pos=Camera.main.ScreenToWorldPoint(Input.toucks[0].position)
将pos.z设置为object.tranform.position.z并将其分配给object
obj.transform.position=pos 那么问题是什么呢?请准确描述问题所在以及您在代码中所做的操作。问题是我无法理解如何从向量2获取输入,并将其作为精灵的位置。至于我的代码,首先我创建了vector 2和sprite变量,然后当屏幕上有触摸时,它会得到触摸的位置,并将其放入vector 2中