- Home /
How to rotate gameobject and rigidbody?
I have a 2D game using physics. I want to manually control rotation and have the rigidbody apply only movement. I have used transform.Rotate to rotate the object, and rigidbody.AddForce(transform.right*power); to apply a thrust. However, this provides a thrust only in the one direction, and doesn't match the direction the object is facing (based on my transform.Rotate).
How do I synchronise rigidbody direction, or force direction, with my gameobject rotation? Thanks!
Edit: Scrap this! It was my fault. My code for limiting top speed was messing it up.
Answer by golden pouria · Jun 27, 2013 at 11:59 PM
Please remember that when the rigidbody.AddForce(Direction * power) function is called, the object will be forced to moved towards the Direction that it is in, at the point that you call the AddForce function. So you call the function only once, and therefore, you pass the direction only once. Regardless of how many times the object rotates.
(in the real world, imagine a basketball, when you throw it, it rotates in the air 3000 times, but that doesn't change the direction of it. The ball moves ONLY towards the direction that you forced it at the time of throwing...)
One of the solutions is that every now and then check the status of the Rotation, and if it is different, stop the object (Neutralize the force) and then call the AddForce again (this time with the new rotation direction) and repeat this process again and again.
Hope this helps.
Answer by robertbu · Jun 28, 2013 at 03:31 AM
By default, Unity's physics engine mimics real physics. Imagine a space ship flying in a certain direction. If you apply thrust to the side of the ship, the ship does not immediately turn 90 degrees. There are a couple of approaches you can use if you are looking for faster turn based on applying force in a new direction. The first is to up the 'Drag' in the rigidbody. 'Drag' is 0 by default. This will cause existing velocity to decay more rapidly so that the new force will take hold faster. If you are looking for an immediate turn you can do something like this in FixedUpdate():
rigidbody.velocity = transform.forward * rigidbody.velocity.magnitude;
This takes makes the ship fly at the existing velocity in whatever direction transform.forward is facing.
Your answer
![](https://koobas.hobune.stream/wayback/20220613105016im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Add Force to the right of the rigidbody, not right of the screen 1 Answer
Can i Move/Rotate triggers without Rigidbodies? And other collider questions. 3 Answers
Pysics not working as expected 1 Answer
Oculus VR Wrist Rotation Relative to Arm 0 Answers
Objects Instantiating at wrong position 3 Answers