简体   繁体   中英

Unity drag and throw a gameobject (ex: sprite) with mouse

I want to throw a gameObject (like a sprite) with my mouse. I would like to handle to power if the mouvement is fast.

I've try to develop it myself but I haven't found yet.

In my mind I think I need to do this :

  • on mouse down : get the position
  • 0.5secs after of on mouse up : get the new position and calculate the distance between the 2 points. Conclude a direction and a force.

What do you think about this ?

I created something fairly similar to this. You should do OnMouseDown() then Raycast on the position of the mouse click. Then your Raycast should grab the collider of the GameObject that was hit. Then you send a message, SendMessage , to that collider telling it what you want to do.

All the links I have given you and some trial an error will help you construct what you need, that is how I created my level editor which is in essence the same thing. Note that all links have code samples that teach you all you need to do. Just use your imagination and you can get it done!

Just start coding. If you get stuck then ask a more specific question and someone will help you. Good luck.

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