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

Resolved Polygon Colldier changes Rigidbody center point

Discussion in '2D' started by devleopment, Mar 1, 2023.

  1. devleopment

    devleopment

    Joined:
    Jan 22, 2023
    Posts:
    2
    Heya!

    I try to create a simple rotation of a "quarter circle sprite" around a full circle which works fine using a rigidbody2d and AddTorque().

    When adding a polygon collider to get physics, the rotation does not work anymore because the polygon collider seems to change the center point of rotation to the polygonized part of the sprite. This only happens on runtime. Rotating the game object in the scene editor works as it should and rotates the whole sprite relatively to its center point.

    Any idea on how I can get both: The rotation of the whole sprite and the polygon collider as seen below? Can I change the center point of rotation with AddTorque() for a Rigidbody2D?

    Thanks in advance!​

     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,954
    One possibility: you don't have the center/pivot set properly in the editor. The button(s) have moved recently so look around, but it used to be near upper left in the editor top bar.

    Otherwise if you're talking runtime, then this is likely the Rigidbody automagically recalculating its center of mass when you add / remove colliders.

    You can set it back where you want, probably to (0,0,0), with an appropriate function call (or maybe a property, I forget). Check out the docs.

    I had to do this for my Jetpack Kurt game to lower the effective center of mass, otherwise the spaceship was too easy to knock over with its bulky collider on top and the spindly legs with almost no mass.
     
    devleopment likes this.
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,622
    The rotational point is always the center of mass because this is physics, not rendering. With no colliders (which is kind of pointless), that'll be a local center of mass of (0,0). If you want to override the local center of mass then you can simply set it using Rigidbody2D.centerOfMass.

    Note that this is nothing to do with the PolygonCollider2D specifically, it applies to all colliders. If you add lots then you'll get their respective center of mass too because this is how physics objects rotate.

    If you look at the Rigidbody2D in the inspector, you'll see an expandable "Info" section. There it shows, amongst other stuff, the local and current world center of mass.
     
    devleopment likes this.
  4. devleopment

    devleopment

    Joined:
    Jan 22, 2023
    Posts:
    2
    Thank you for your quick feedback. I now set the "centerOfMass" to Vector.zero and it works like a charm.

    See you later :)
     
    MelvMay likes this.