- Home /
How to rotate a sphere from its center while moving
Right now I am trying to make it so that my sphere moves forward while rotating, like a ball rolling down a hill. So I made a script for it to roll and move.
function Update () { transform.Translate(Vector3(-10,0,0) Time.deltaTime); transform.Rotate(Vector3(0,0,-10) Time.deltaTime); }
Now if the ball is not moving, the rotation of the ball is at its center, but if the ball is moving, it just does a giant loop while moving. Does anybody know a solution to this? Thanks.
Answer by CodeMasterMike · Jan 28, 2013 at 09:15 AM
I'm suspecting that you are rotating the ball in the world coordinates. You should try to rotate it in the local coordinates instead:
transform.Rotate((Vector3(0.0f, 0.0f, -10.0f) * Time.deltaTime), Space.Self);
Good luck!
Close enough. Rotate's space is self by default. And so is translate ! Change the Translate's space to world ins$$anonymous$$d of Self, or the rotation will affect the translate's direction.
http://docs.unity3d.com/Documentation/ScriptReference/Transform.Rotate.html
http://docs.unity3d.com/Documentation/ScriptReference/Transform.Translate.html
Answer by Khyrid · Jan 28, 2013 at 11:43 AM
Space.Self was important but I also had to make a parent object that wouldn't be affected by rotation and have the child of it be the one to rotate. I wasn't able to have the same object move and rotate without it losing its center as the pivot. Not a big issue.
Your answer