Search Unity

Unable to Rotate object around its center

Discussion in 'General Graphics' started by ron2468, Jan 14, 2020.

  1. ron2468

    ron2468

    Joined:
    Jan 11, 2020
    Posts:
    3
    Hi all,
    I'm not sure where to post this, but here goes...

    I can't figure out why this layer is not rotating correctly around its center? (see attached picture).

    Here's a snippet of my code:

    void Start()
    {
    centerOfMass = transform.position; //<== get the current position of the RIGHT Layer
    }
    transform.RotateAround( centerOfMass, Vector3.forward, rotationspeed );

    When I press the R button, it's suppose to rotate 90 degrees clockwise, but the final position is offset as u can see in the picture. When I click the button a 2nd time (for a total of 180 degrees rotation), it's offset again, and offset again at 270, but does return to normal position at the 4th click (completing the 360 degree rotation).

    It appears the layer is rotating around a point that is NOT its center, right? So it looks like the transform.position does NOT correspond to the center of the Layer object.

    If so, then how do I programmatically obtain the center of mass or center point of an object?

    I'd GREATLY appreciate any help!!

    Ron
     

    Attached Files:

  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    The
    transform.position
    is the transform's pivot position. If you want the "center of mass" then you want to take the center of its bounds. For what you're doing, you probably want to use a game object with its position at the exact center of your Rubix cube, since that'll be the pivot for any rotation.
     
    bartekpacia likes this.
  3. ron2468

    ron2468

    Joined:
    Jan 11, 2020
    Posts:
    3
    Interesting. Ok, I'll try that.
    Thank you!