- Home /
Dragging a 2D sprite with touch
I've been searching all day, but nothing seems to work. As the title says I want to be able to drag a 2D sprite with touch input. i tried a lot of stuff so one code example wouldn't make sense, but I just want the transform.position of the 2D object changed to where the finger touches the screen.
Answer by NikkF · Mar 23 at 10:55 PM
You'll want to use https://docs.unity3d.com/ScriptReference/Touch-position.html ,
like this:
if (Input.touchCount > 0) {
Touch touch = Input.GetTouch(0);
sprite.position = touch.position;
}
Touch is where the play is currently touching the screen, it gives you a full vector that you can make the sprite follow in the Update() method.
Hope this helps!
Your answer
Follow this Question
Related Questions
I am making a 2d game and I want to make it where to can pick up bricks with your mouse 3 Answers
Need help in managing sprite transform. 1 Answer
transform.position returning wrong value (UNITY 2D) 0 Answers
How can i fix a position of a gameobject after it meets its condition 1 Answer
Move object by touch input 5 Answers