---
tags:
    - "Physics, 2D"
---

# Set Rigidbody 2D Center Of Mass

[Physics 2D](../index.md) / [Rigidbody 2D](index.md) / **Set Rigidbody 2D Center Of Mass**

## Description

Sets the Center Of Mass property of Rigidbody 2D component

## Parameters

| Name | Description |
| :--- | :---------- |
| Rigidbody 2D |  |
| Value |  |

## Code

```cs
// <auto-generated>
// This script was automatically generated using the Visual Script Transpiler.
// Do not modify. Changes to this file will be lost when updating the package.
// </auto-generated>

using System;
using UnityEngine;
using VisualScript.Icons;

namespace VisualScript.Runtime
{
    [Title("Set Rigidbody 2D Center Of Mass")]
    [Category("Physics 2D/Rigidbody 2D/Center Of Mass")]
    [Description("Sets the Center Of Mass property of Rigidbody 2D component")]

    [Image(typeof(IconPhysics), ColorTheme.Type.Lime, typeof(Badge2D))]
    [Keywords("Physics, 2D")]

    [Parameter("Rigidbody 2D")]
    [Parameter("Value")]

    [Serializable]
    public class GenInstructionVector2_Rigidbody2D_SetCenterOfMass : Instruction
    {
        [SerializeField] private GetGameObject m_Rigidbody2D = new GetGameObject(
            new GenTypeGameObject_Rigidbody2D()
        );

        [SerializeField] private GetVector2 m_Value = new GetVector2();

        [SerializeField] private TweenTransition m_Transition = new TweenTransition();

        [NonSerialized] private bool m_IsComplete;

        protected override string GetTitle(GameObject source)
        {
            string member = TextUtils.ThemeAsKeyword("Center Of Mass");
            return $"{this.m_Rigidbody2D} {member} = {this.m_Value}{this.m_Transition}";
        }

        [NonSerialized] private Action<Component, Vector2> m_CallOnUpdate;
        [NonSerialized] private Action m_CallOnFinish;
        [NonSerialized] private Func<bool> m_CallIsComplete;

        protected override async Awaitable Run(Args args)
        {
            Rigidbody2D component = this.m_Rigidbody2D.Get<Rigidbody2D>(args);
            if (component == null) return;

            bool wait = this.m_Transition.Await;
            this.m_IsComplete = false;

            this.m_CallOnUpdate ??= this.OnUpdate;
            this.m_CallOnFinish ??= this.OnFinish;
            this.m_CallIsComplete ??= this.IsComplete;

            TweenManager.Run(new TweenInstanceInput<Vector2>(
                component,
                "centerOfMass",
                component.centerOfMass,
                (Vector2) this.m_Value.Get(args),
                this.m_CallOnUpdate,
                wait ? this.m_CallOnFinish : null,
                this.m_Transition.GetDuration(args),
                this.m_Transition.EaseMode,
                this.m_Transition.TimeMode
            ));

            if (wait) await this.Until(this.m_CallIsComplete);
        }

        private void OnUpdate(Component component, Vector2 value)
        {
            if (component is not Rigidbody2D componentRigidbody2D) return;
            componentRigidbody2D.centerOfMass = value;
        }

        private void OnFinish() => this.m_IsComplete = true;

        private bool IsComplete() => this.m_IsComplete;
    }
}
```

