- Home /
Unity2D - how to move object to the touched position
I'm using my android device to detect the touched position and move my object, but my object change the position way to far it disapears from my camera view, do I need to use ScreenToWorldPoint? It so, how do I use it?
void Update() {
for (var i = 0; i < Input.touchCount; i++) {
if (Input.GetTouch(i).phase == TouchPhase.Began) {
transform.position = new Vector3 (Input.GetTouch(i).position.x, Input.GetTouch(i).position.y, transform.position.z);
}
}
}
Answer by zulteon · Jul 21, 2016 at 04:27 PM
Hello! I think you already know the solution just too lazy for google it. :P
transform.position = Camera.main.ScreenToWorldPoint ( Input.GetTouch(i).position );
However if you touch with multiple finger it may cause you a problem. Your object don't know where to go. You can check in your if statement : if (Input.GetTouch(i).phase == TouchPhase.Began && i==0 ) {
so you only dealing with the first touch. also if you want to move multiple object sooner or later. You should check if the user want to move this specific object.
Hope i helped something Best wishes
Your answer
![](https://koobas.hobune.stream/wayback/20220612085229im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
C# Serialization is working fine in Unity but not on Android 2 Answers
Android build misbehaving. 1 Answer
Help with DPAD Input Touch For Mobile 0 Answers