Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Center of mass not so accurate.

Discussion in '2D' started by notgrantcawood, Aug 11, 2014.

  1. notgrantcawood

    notgrantcawood

    Joined:
    Feb 1, 2014
    Posts:
    13
    I'm using this to keep updating the center of mass so that when the user rotates, it will always rotate from the center of the screen.

    Code (CSharp):
    1. Vector2 inversePos = cam.InverseTransformPoint(transform.position);
    2. rigidbody2D.centerOfMass = -inversePos;
    Using debug.log I can see that the center of mass is updating correctly but it seems that it's always just slightly off center. And if it's rotated for a long time, it will get worse.

    I did notice in the console that the center of mas was being updated to one decimal point. I thought maybe for more accuracy, I would need at least a couple more.

    Is there a way to increase the precision?
     
  2. Punchbag

    Punchbag

    Joined:
    Oct 30, 2012
    Posts:
    48
    I don't know whether there's a way to increase precision, but you're obviously getting a cumulative error because of it. You could try storing the original center of mass and always adding the calculated value to that, instead.

    Are you sure that this is the best approach for keeping something rotated in the centre of screen? If you provide a quick vid or animated gif of the scenario, I might be able to suggest a cleaner approach.
     
  3. notgrantcawood

    notgrantcawood

    Joined:
    Feb 1, 2014
    Posts:
    13
    t
    Thanks for the reply.
    I ended up putting an empty game object in the middle of the screen and used rotatearound. It works perfect for what I need.
     
  4. Punchbag

    Punchbag

    Joined:
    Oct 30, 2012
    Posts:
    48
    That sounds like a solid approach, nicely done!