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.
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!