Reputation: 29
I want to move object on touch input in android for unity 2d in c#
I have a pin object and want to move that pin on touching it and moving it as per the touch moves and stop the object where the touch drops it.
also i want to check that pin moved by touch is dropped at certain points on the screen,say for example another box objects where you need to drop the pin object.
I am a newbie to unity platform and is struggling for touch input movement of object.It's and android game. i donot want to use touchscript from asset store of unity for touch in android.I need a script so that i can attach it to object. Please help.
Upvotes: 0
Views: 3104
Reputation: 66
You can use OnMouseDrag() because it works on both the editor and mobile so it's easier to test. It works for 3D objects as well as long as they have a collider.
Create a script.
Add the function below to the script and attach it to your sprite.
Make sure to add a collider2d to your sprite otherwise it won't work.
void OnMouseDrag() {
Vector3 mousePosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, -1*(Camera.main.transform.position.z));
Vector3 objPosition = Camera.main.ScreenToWorldPoint(mousePosition);
transform.position = objPosition; }
Upvotes: 1