- Home /
Rotating, moving and keep on going with constant velocity in direction of click
Hi, I am trying to create a 2D object which rotates and move in the direction of mouse click/touch. Also, I want this object to keep on going with constant speed in same direction even if it reaches the click/touch position. It should only change direction when user again clicks/touches the screen. I really appreciate all the help, thank you.
mousepos=convert mouse position to worldspace; objectpos=object.transform.position; velocitydir=(mousepos-objectpos).normalize;
object.rigidbody2d.velocity=velocitydir*your$$anonymous$$ultiplierforVelocity
Answer by Propagant · Jan 18, 2018 at 05:58 AM
Hey, so first of all you have to create 'LookAtCursor' method... Check this out: https://answers.unity.com/questions/10615/rotate-objectweapon-towards-mouse-cursor-2d.html or https://answers.unity.com/questions/653798/character-always-facing-mouse-cursor-position.html
This can be implemented on any object that will follow the cursor. And at last, add transform.position+=transform.forward; to start moving forward [following cursor]
Your answer
Follow this Question
Related Questions
Move while mouse button is held down 3 Answers
How to properly set the position clicked by the mouse? 0 Answers
OnMouseUp() Click Display Effect. 1 Answer
Programming Movement Momentum 0 Answers