Rigidbodyの重心をずらすスクリプト(Unity)
まずは実行結果を示します。
だるまにRigidbodyをつけただけ pic.twitter.com/Rh2icaHJiO
— be116@home (@co_Guts) 2018年5月7日
重心をちゃんとずらしただるま pic.twitter.com/fSCneEwfvQ
— be116@home (@co_Guts) 2018年5月7日
使用したスクリプトはこれになります。 といっても、ここ(https://docs.unity3d.com/ja/current/ScriptReference/Rigidbody-centerOfMass.html)の改変です。
using System.Collections; using System.Collections.Generic; using UnityEngine; /// <summary> /// 重心の位置を変更 /// </summary> public class CenterOfMass : MonoBehaviour { /// <summary> /// 重心の位置 /// </summary> public Transform com; /// <summary> /// オブジェクトがEnableになったとき /// </summary> void OnEnable () { Rigidbody rb = GetComponent<Rigidbody>(); rb.centerOfMass = com.localPosition; } }
このスクリプトは、重心をずらしたいゲームオブジェクトにアタッチして使います。 このゲームオブジェクトの子に新たにゲームオブジェクトを作成し、 Com変数に作成したオブジェクトをアタッチします。 このオブジェクトが新しい重心となり、任意の位置に動かすことで重心をずらすことができます。