- Home /
Why am I getting 2 different results with RotateAround? Trying to move cubes on surface of sphere.
I have the following line in a script on cubes that are spawned as children on the surface of a sphere. transform.RotateAround (Vector3.zero, -Vector3.up, -5);
It works relatively well however I want the cubes to rotate around an axis parallel to their forward vector that goes through the center of the sphere they are on. I tried the following
transform.RotateAround (Vector3.zero, transform.forward, 5);
This makes the cubes rotate about an axis going through THEIR center and doesn't move them along the sphere. With the second approach I feel that the behavior of RotateAround is somehow changing. Regardless of where the cube is on the sphere they are only rotating about their own axes.
Am I missing something about how this works? Please let me know if I can clarify.
Answer by robertbu · Jun 26, 2013 at 07:08 PM
A couple of things. Position matters. If you place your block on top of the sphere to start, then your rotation should work. Next, if the sphere is not rotated during the game, consider using Vector3.forward instead of transorm.forward. Also you should be multiplying by Time.deltaTime to keep your code frame rate independent.
transform.RotateAround (Vector3.zero, Vector.forward, 50 * Time.deltaTime);
Thanks for bringing up the sphere rotation - I should have mentioned that. The sphere is constantly rotating about its x axis. The blocks are spawned at 0,0,1 (the top of the z side of the cube). This code is also executed on button press. I want all the cubes to do the rotation when the button is pressed. Because of this I thought that I didn't need Time.deltatime, IE all the blocks rotate 5 degrees to the right on the sphere when I press D. Thanks a lot for the answer - this was my first UnityAnswers question and I'm surprised that it was addressed so quickly.
the blocks spawn at 0,0,1 (z side of the sphere) and you mentioned an important point - the sphere that the blocks rest on is constantly rotating about its x axis. This line of code is called on button press and isn't executing every frame so I didn't think I needed Time.deltatime. I guess the important part is that the sphere is always rotating - that's probably where the issues is co$$anonymous$$g from.
If the sphere is rotating, and you are using transform.forward to rotate around, then you want to use transform.up as the place to instantiate your blocks. If the sphere has no rotation, and you spawn at (0,0,1), and you use Vector3.forward, you will get the behavior you describe above where the block appears to rotate around its own axis..
If you are doing an immediate rotation of 5 degrees on a single event, then you are right that you don't need to use Time.deltaTime.
Your answer
Follow this Question
Related Questions
Rotate Around an Object using a different pivot point? 0 Answers
Apply torque/force to rotate an object around a fixed point 1 Answer
Quick Shortcuts for Rotation 0 Answers
RotateAround x degrees over y time 1 Answer
transform RotateAround 1 Answer