- Home /
Rotating a Rigid Body and moving Forward
var yTurn = 0; //adjusted left/right arrow var zSpeed = 0; // adjusted by up/down arrow var playerMarble : GameObject; //marble object var playerMove : gameObject; // empty game object that is used for movement of all children
playerMarble.transform.RotateAround(playerMove.transform.position, Vector3.up, yTurn); playerMarble.rigidbody.AddForce (Vector3.forward * zSpeed);
Here is a bit of the code I'm using to move and rotate my marble. The move forward works, the rotation works, but the move forward always move one direction no matter where it's rotated. So even if I rotate it 90 degrees it still moves forward in the original direction before the rotation.
What I'd like is the rotation to determine which direction the force is added. I thought forward would always move on the Z axis which is what forward would be. Any help would be great.
Answer by Uriel_96 · Dec 12, 2010 at 09:51 PM
OK your problem is simple. You in add force you put Vector3.forward, OK so thats rong because the Vector3 moves in the vectors it mines it dosent matter how you rotate it dosent go the forward you want so you need to change Vector3.forward to transform.forward
Thanks man! I've been searching for this for hours, lol. At first, your solution didn't work. But now I realized I problably had to multiply transform.forward by something, and it worked. Thanks :)
Answer by Proclyon · Dec 12, 2010 at 09:54 PM
Forward is an absolute value not a relative one which is the information you asked for. Getting such a value can be achieved by giving the force over the same axis the same force but added with the rotation of the object.
Say the object is rotated 90 degrees around the Y axis and faces (absolute) right. That means adding force forward (absolute value) will increment the force in the angle of the axis itself without the rotation (0 value). Simply add the rotation value to the force
So should I go back to using my own code for force and use transform.rotate to move my marble ins$$anonymous$$d of using AddForce?
Or how do you add rotation to the force?
That's question 2: I found some similar ones for you that answer that question, here http://answers.unity3d.com/questions/18774/unprecise-rotation-when-adding-a-relative-force-to-a-rigidbody, and here, http://answers.unity3d.com/questions/8302/addrelativeforce-miscalculations
Answer by kachmar.bohdan · Nov 09, 2011 at 04:18 PM
You can try to add force relatively using Rigidbody.AddRelativeForce() Look more here: http://unity3d.com/support/documentation/ScriptReference/Rigidbody.html
Answer by Hamilcar-games · Jul 29, 2017 at 07:14 PM
make a child game object which holds the mesh render than rotate it with a seperate scriipt