Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Rotating GameObject not around center of gravity as expected

Discussion in 'Scripting' started by Alai, Aug 31, 2009.

  1. Alai

    Alai

    Joined:
    Feb 14, 2009
    Posts:
    15
    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;

    // Use this for initialization
    void Start ()
    {

    }

    void Update()
    {
    this.inputXAxis = Input.GetAxis("Horizontal");
    this.inputYAxis = Input.GetAxis("Vertical");

    float rotateX = this.inputYAxis * Time.deltaTime * 10;
    float rotateY = this.inputXAxis * Time.deltaTime * 10;
    float rotateZ = 0;

    if (
    (0 != rotateX) ||
    (0 != rotateY) ||
    (0 != rotateZ)
    )
    {
    Debug.Log("Rotating " + this.gameObject.name + " to (" + rotateX + "," + rotateY + "," + rotateZ + ")");
    this.gameObject.transform.Rotate(rotateX, rotateY, rotateZ);
    }
    }
    }
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    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.
     
  3. Alai

    Alai

    Joined:
    Feb 14, 2009
    Posts:
    15
    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?
     
  4. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    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.
     
  5. Alai

    Alai

    Joined:
    Feb 14, 2009
    Posts:
    15
    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...
     
  6. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    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.
     
  7. Alai

    Alai

    Joined:
    Feb 14, 2009
    Posts:
    15
    Thanks. When I'm back in range of my computer I'll put up the information. It will probably be late tonight.
     
  8. Alai

    Alai

    Joined:
    Feb 14, 2009
    Posts:
    15
    Here are the screenshots
     

    Attached Files:

  9. Alai

    Alai

    Joined:
    Feb 14, 2009
    Posts:
    15
  10. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    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.
     
  11. Alai

    Alai

    Joined:
    Feb 14, 2009
    Posts:
    15
    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.
     
  12. jmunozar

    jmunozar

    Joined:
    Jun 23, 2008
    Posts:
    1,091
    Hello,

    Can you please make those images smaller?, it helps out the people who read the forum :)
     
  13. Alai

    Alai

    Joined:
    Feb 14, 2009
    Posts:
    15
    Sorry; I didn't realize they would appear inline like that until I posted them.