Unity/3d/グローバルとローカル座標変換
提供: 初心者エンジニアの簡易メモ
サンプル
// ローカル座標をグローバル座標を Vector3 parentPointGlocalPosition = parentObj.transform.TransformPoint(childObj.transform.localPosition); // グローバル座標をローカル座標に Vector3 parentPointLocalPosition = parentObj.transform.InverseTransformPoint(childObj.transform.position);
参考:https://nekojara.city/unity-transform-point
オブジェクト内の子オブジェクトのグローバル座標が、0となるように、親オブジェクトを動かす方法
GameObject childObj = GameObject.Find("Child").transform.gameObject;
GameObject parentObj = GameObject.Find("Parent").transform.gameObject;
Vector3 parentPointLocalPosition = parentObj.transform.InverseTransformPoint(childObj.transform.position);
parentObj.transform.localPosition = parentPointLocalPosition;
parentObj.transform.rotation = childObj.transform.rotation;
オブジェクトのstaticにチェックが入っていると動かないので、注意。
