Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Center of mass not working

Discussion in 'Physics' started by Antiqxes, Feb 21, 2022.

  1. Antiqxes

    Antiqxes

    Joined:
    Apr 23, 2021
    Posts:
    1
    I made a simple script for a car I have wheel colliders and they work fine, but when I try to define the center of mass the car stands up when I press start upload_2022-2-21_9-29-30.png
    With center of mass defined: upload_2022-2-21_9-31-20.png
    Without center of mass defined: upload_2022-2-21_9-31-53.png
     
    Thran likes this.
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,558
    The center of mass being configured way behind the car's rear axle. I can guarantee you that Rigidbody.centerOfMass works perfectly. The problem is in your side, verify your scene for:
    • Wrong GameObject assigned to your script's centerOfMass property.
    • Incorrect position of the GameObject specified as center of mass.
     
  3. Thran

    Thran

    Joined:
    Apr 22, 2016
    Posts:
    2
    i have the exact same problem

    i know this thread is 2 years old. did you already find a solution to your problem?

    If i set my Center of Mass somewhere close to 0, 0, 0, my car will flip back like in your pictures.

    when i set the Center of Mass far in front, in the middle of the street, far ahead of my car, like 0, 0, 8, it will show the center of mass of the rigidbody as 0 and everything will work fine.

    something adds hell of a mass to the backside of my car and i dont know what. its just made out of 4 wheels and a camera right in the middle of it. there is nothing at the back that could add mass to 0,0,-8 for it to flip back
     
  4. codebiscuits

    codebiscuits

    Joined:
    Jun 16, 2020
    Posts:
    118
    Regarding the OP, shouldn't it be:
    Code (CSharp):
    1. playerRb.centerOfMass = playerRb.transform.InverseTransformPoint(centerOfMass.position);
    ?
    Or, as a shortcut, if the centerOfMass GameObject is a child of the playerRb, just:
    Code (CSharp):
    1. playerRb.centerOfMass = centerOfMass.localPosition;
    I think the original problem was centerOfMass.transform.position is in world space, but playerRb.centerOfMass is in local space.
     
    Thran likes this.
  5. Thran

    Thran

    Joined:
    Apr 22, 2016
    Posts:
    2
    Thanks! that helped.
    Code (CSharp):
    1. playerRb.centerOfMass = centerOfMass.transform.localPosition;
    the centerOfMass Gameobject was a child of the car, but we (OP and I) both used the global position,

    Thats actually what the official Unity Tutorial does aswell. They used the global position in their tutorial video (which is from 2024, so pretty new, but they use Unity2018 in the video.)

    here the link to the video in question, in case other people have the same problem: https://learn.unity.com/tutorial/le...2adcedbc2a0e5c02d26f#658f5be1edbc2a255f0a71fb
     
    Last edited: Mar 6, 2024
    codebiscuits likes this.