Drag to Aim and Release to Shoot?
I am creating a clone of Soccer Stars and I would like to have my mouse drag on the piece and when the mouse either lets go of the left button or it reaches the max distance I allow, to apply force proportional to that to the puck and have it move. How would I go about doing this? Example code is very helpful. :)
Answer by dhore · Feb 12, 2016 at 11:08 AM
You can use the OnMouseDown() function to detect when the user clicks on a given object. Then within that function use either the mouse's position or the object's position in screenspace to store the location of the click - you'd use the object's position if you want any GUI arrows to be in the centre of the object (for a nicer UI).
Then while the mouse is still being held down (determined by a boolean you'd set to true in the above function) you want to keep track of the mouse's position and use it to move any GUI arrows around or whatever. (Note: do not override the location of the initial mouseDown click; you will need that one later).
Use the OnMouseUp() function to detect when the mouse is released and store the final mouse position for the following calculations: Use some simple Vector maths to determine the distance and direction between the initial mouseDown click and the final mouse position when it was released - you can then use these to determine the force and direction to shoot your pucks.
Your answer
Follow this Question
Related Questions
Click or Drag but not both 1 Answer
,Know if the mouse position has changed 0 Answers
Hovercar rigidbody acceleration 1 Answer