How to add custom Center Of Gravity to any Unity GameObject

  1. Unity 2017.x or better
  2. Visual Studio/VsCode (I will be using Visual Studio to demonstrate)
  3. Basic Understanding of Unity Editor and C#
  • First Open Unity Hub and Create a New Project
Create a new unity project
Importing the model
  • If the model you imported has colliders included in it, you are good to go and skip to the next part. As, I don’t have colliders with this specific model I will add two box colliders in this model, one for the base and one for the upper portion.
Adding Box collider Component
After adding the Box collider Component
  • Now, let’s add the Rigidbody Component to the GameObject. Select the GameObject, Go to the inspector, and click to the Add Component Button, and Select Rigidbody
Click on the Add Component
Type Rigidbody and add it to the Object
  • Now if we hit play, we will see that the “seesaw” stands still as our Rigidbody Component has calculated the Center of Gravity from the Colliders.
Center Of Gravity is in the middle
  • Now let’s add our custom Center of Gravity. To add custom Center of Gravity let’s add a new empty GameObject and call it “COG”
Adding a new empty GameObject
  • Now, Let’s assume we want the Center of Gravity to be on the left side of the model. So, select the Move Tool and place it on the left of the model.
Placing the COG to the left
  • Now, all we need to do is to code and attach this “COG” as the Center of Gravity of the model. For that create a new Script and Call it, let’s say “CustomCenterOfGravity” and open it on Visual Studio/Vscode.
Creating a new Script
Open the script
  • We will access the Rigidbody Component of the 3d “seesaw” model with this script and set the Center Of Gravity of the Rigidbody to the location of the “COG” Gameobject that we created.
    So, let’s first create a public variable Rigidbody and name it rb. and get a reference of the Rigidbody component from the Gameobject on the Start Method. Also, we need to create another variable of transform type that will hold the transform information of the “COG” GameObject.
using System.Collections;using System.Collections.Generic;using UnityEngine;public class CenterOfGravity : MonoBehaviour{    private Rigidbody rb;
public Transform centerOfGravity;
// Start is called before the first frame update void Start() { rb = GetComponent<Rigidbody>(); }}
  • Now, we will check if the centerOfGravity variable is null or not. If it is not null then we will assign the center of gravity of the Rigidbody Component of our 3d model to the location of our “COG” GameObject.
using System.Collections;using System.Collections.Generic;using UnityEngine;public class CenterOfGravity : MonoBehaviour{    private Rigidbody rb;    public Transform centerOfGravity;// Start is called before the first frame update    void Start()    {        rb = GetComponent<Rigidbody>();        if (centerOfGravity)        {            rb.centerOfMass = centerOfGravity.localPosition;        }    }
}
  • Here we accessed the centerOfMess of the 3d model with “rb.centerOfMass” and assigned it to the local position of the “COG” GameObject with “centerOfGravity.localPosition”
  • Now, all we have to do is assign the script to the 3d model first and then assign “COG” GameObject to the centerOfGravity variable of the script by dragging and dropping it on the inspector.
Add the Script to the 3d model
drag and drop the “COG” component on the script
  • And that’s it, the CenterOfGravity is now changed. It is now on the location of the “COG” object.

--

--

--

The least cool person ever to walk on this planet

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Primal Data Advent Calendar #1: How to skip Airflow tasks conditionally

FinTech Mentor Job Opportunity

Quote Do, Unquote, Use, Require, and Import?

He Who Writes the Least Code Wins

The Cost of the Cloud: Here’s What You Need to Know about AWS Pricing

Feature Request: Dismiss Tag

CS371p Spring 2021: Week 2

What’s Buzzing ML vs AI vs Cloud Computing?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Hasi-b Hasan

Hasi-b Hasan

The least cool person ever to walk on this planet

More from Medium

Tiling a Procedurally Generated Unity TileMap

A quick guide to player setup in Unity

Use NuGet packages with Unity

Improving Upon Selection Highlighting in Our Game — Part 2