一 : 意义
TransfromPoint,将3D物体的本地坐标转化成世界坐标。例如: TransformPoint(1,0,0)既是将3D物体本地坐标(1,0,0)的点转化成世界坐标
二 : 实践
1⃣️,获取Cube右前方45度10米处的世界坐标。
1, 作图如下:
2⃣️, 设计测试
1, 场景,放一个球体在World坐标系的原点。在cube上挂载一个脚本如下:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class TFPDemo : MonoBehaviour { private float? targetX; private float? targetZ; private Vector3 targetWorldPosition; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { this.targetX = Mathf.Cos(45 * Mathf.Deg2Rad) * 10; this.targetZ = Mathf.Sin(45 * Mathf.Deg2Rad) * 10; this.targetWorldPosition = this.transform.TransformPoint((float)this.targetX, 0, (float)this.targetZ); Debug.DrawLine(Vector3.zero, this.targetWorldPosition, Color.red); } }
3⃣️,测试结果 (和预想的一致)