简体   繁体   中英

how to move object in unity 2d using touch input in c#

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.

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; } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM