- Home /
Moving a rigidbody towards the mouse position
Hello everyone! I would like to know if anyone could help me with a bit of an issue (obviously :p).
The set up I currently have moves the player (a sprite) towards another object that is set to the mouses position on the x and y axes (yes, in case you didn't know, that is the plural of axis). I accomplish this with the "Vector3.moveTowards()" function. Now, this works great, the movement is nice and smooth and almost everything is suitable. Almost. The issue I'm running into is collion, specifically the player being able to move the ship out of the bounds of the screen. So I was hoping maybe there was a way to move a rigidbody (the player in this instance) towards an object (the object that is set the mouses position) without rotating the rigidbody? That is probably very vague, but that is the only way I can think of describing the issue.
Any assistance would be much appreciated.
Answer by Torigas · Jul 23, 2015 at 08:12 AM
rigidbody.ApplyForce(directionVector);
and a specific ForceMode (Impulse). You'd just have to make sure you stop it when you reach the target.
rigidbody.ApplyForce(rigidbody.velocity *-1, ForceMode.ChangeVelocity);
or
rigidbody.velocity = Vector3.zero
Thanks a lot! Everything works the way I want it to now!
Your answer
Follow this Question
Related Questions
Player is Speeding up in collisions 0 Answers
How do you move a character with physics when using Animator.setInteger to play the animations? 0 Answers
Stopping my player from moving when hitting a wall. 5 Answers
Character controller jittering up and down 2 Answers
Slope limiting with Rigidbody 0 Answers