Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Center of mass on 2D object

Discussion in 'Scripting' started by GFFG, Mar 12, 2017.

  1. GFFG

    GFFG

    Joined:
    Nov 13, 2014
    Posts:
    224
    Hi,

    I'm making a little 2D side scroller game and i'd like to control the center of mass or weight distribution of my flying object. In the scripting API the info is sparse "public Vector2 centerOfMass" and I'm not that proficient in programming. Can anybody help me by telling me how I can set an objects center of mass to the transform position? That way I'd be able to see how the object flies optimally depending on the weight distribution.

    Since it's a 2D game the weight distribution could only either be in the front, middle or back of the object, which should simplify things.
     
  2. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,772
    myRigidBody2d.centerOfMass = Vector2.zero;

    The center of mass is defined in local space so setting it to zero should set it to the rigidBody's transform's position.
     
  3. GFFG

    GFFG

    Joined:
    Nov 13, 2014
    Posts:
    224
    Thanks, I'll try it out :)