I have a GameObject hierarchy which I am rotating by rotating the root GameObject of the hierarchy with transform.Rotate. However, instead of rotating around its center of gravity (the top-level bare GameObject (no mesh)) as expected, it appears to rotate around a point in front of it which is definitely not where the top-level object is.
I have the script below attached to the top-level object that I want to rotate.
Any ideas?
Thanks.
Richard
public class PlayerControlScript : MonoBehaviour {
private float inputXAxis;
private float inputYAxis;
If this script is attached to the root object, then it should certainly rotate around the object’s anchor point. If the object has no mesh then presumably you are viewing the anchor point rotating in the scene view. Make sure you have the root object selected when viewing. Also, just a little tip - the MonoBehaviour class has a transform property, so you can just write transform.Rotate rather than this.gameObject.transform.Rotate.
Hmm… when I select the root object in the scene, it’s definitely not rotating around the point that it highlights… I’ve thought that the point indicated in the scene would be the object’s anchor point. If that isn’t it, what would it be?
It usually rotates around the point declared to be 0,0,0 in the model’s 3D modeling application.
If you want it to rotate around a specific point of your choosing, just edit the 3D model or create an empty game object, put it where you want the pivot point to be, stick the model inside as a child of the empty, and then rotate the empty.
Hmm, what you describe sounds like what I’m doing, I think. Unfortunately, I’m not in range of my computer that has Unity on it, but my hierarchy looks like this:
Player Ship (empty, contains rotate script that should rotate around this, as indicated above)
→ Engine (child of Player Ship)
→ Engine (child of Player Ship)
→ Main Camera (child of Player Ship)
→ Gun Cube (child of Player Ship)
All children are connected to their parents by rigid joints.
What is the situation where it rotates around 0,0,0? That sounds like what I’m seeing, even though I’m explicitly rotating that empty GameObject which parents everything…
Well, the rigid joints may mess up the rotation, for one thing. However, whenever I set up an empty at the spot I want rotated around, it rotates around it just fine. I have a feeling I’d need to see your setup.
You could try removing the joints and seeing what happens, though. Also, it’d be nice to see a screenshot of the object, with empty selected, at two different rotations so we can see what’s going on.
Looks normal to me! Both the ships rotate around the axis that pops up when you select the parent for the group. The empty for each of them is nowhere near their center, so they rotate around it instead of rotating in place.
Normal behavior. Just move the empty parent closer to the ship pieces if you want more natural rotation.
Thanks. I finally just removed all the children to see where the empty really was after you said that.
This leaves me wondering why there isn’t any visual indication in the GUI as to where the empty’s center is. It was confusing because I couldn’t see the actual empty position in the GUI, so I thought that the empty position was where the move arrows for the empty originated from.