- Home /
How to move an object towards the touch position?
Hello everyone, I'm trying to make a 2D game that can run on Android. I have a 2D sprite that I want to move towards the touch position. (the sprite is not exactly 2D, because the z-axis is -0.001). I have tried many scripts that i found online, but they aren't working. When i touch the screen, the sprite doesn't moves to the touch position, but it goes to random places. Here is one if the scripts that i tried: https://docs.unity3d.com/ScriptReference/Input.GetTouch.html. I hope you guys can help me.
Eh.... Good day.
Learn to code, fropm the beginign. Dont want to simple copy psate code if dont know what it does...
I can understand what you are saying, but i have also tried self-made codes like this:`transform.position = new Vector2(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y);` The problem with this code is that it will do the same things and it will place the z-axis to 0 and that means that the sprite will go behind the background.
For the problem that your sprite is behind other stuff. I think the best way to solve would be to play around with the Sprite Renderer's Order in Layer and Sorting Layer. If that doesn't work simply set the transform with a Vector3 like that
transform.position = new Vector3(yourX, yourY, -0.001);
Having the same problem but with a 3d object, no experience using screentoworldpoint so im very confused how to get it to follow my character.
So you want a 3D Object to follow your touch input? If so, I think it would be the best if you quickly phrase a new question and I'll answer it if I see it (:
That would be amazing I just posted it a little while ago on my account, was looking through to see if there were any questions I could possibly answer in the meantime. Thanks!
Answer by Der-Die-Das · Sep 13, 2018 at 06:48 AM
Your problem is probably that you do something like that:
transform.position = Input.GetTouch(0).position;
Which is wrong since the position you get from the Input System is ScreenSpace and your transform Object is World Space. You might try something like:
Camera cam = Camera.main;
transform.position = cam.ScreenToWorldPoint(Input.GetTouch(0).position;
Be aware that this is untested code! And if you do that in Update you should probably cache the cam variable.
Hope that helps!
This was the code that i have tried, but wasn't working: transform.position = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position); It is similar to your code, but both codes did not work. Then I noticed that your code changes the position like it is a Vector2, but i need Vector3 because of the z-axis. Than i have changed the code to this:
Camera cam = Camera.main;
transform.position = cam.ScreenToWorldPoint(Input.GetTouch(0).position);
transform.position = new Vector3(transform.position.x, transform.position.y, -0.0001f);
Now I have the right code, but thanks to your answer I now know what I did (partially) wrong. Thank for Helping me. Have a good day :)
Your answer
![](https://koobas.hobune.stream/wayback/20220612174459im_/https://answers.unity.com/themes/thub/images/avi.jpg)