Unity如何获取BoxCollider八个点的世界坐标
发表于2017-11-01
项目中想将场景中的BoxCollider的坐标点导出出来让服务器也用用,虽然BoxCollider自身的大小可以获取出来,但是如果有父对象,并且有坐标的变化那算起来就比较麻烦了。幸好Unity提供了一个方法,通过8个标记点来标记获取到boxcollider的坐标。这样不管旋转、缩放都可以计算出正确的坐标点来。如下图所示:
using UnityEngine; using System.Collections; public class Test : MonoBehaviour { //8个标志位 ,用来在scene里预览 public Transform[] points; //需要提取Boxcollier顶点的对象 public BoxCollider cube; void Start() { //父节只能调节位置, 不能调节旋转和缩放。 Transform parent = cube.transform.parent; while(parent!=null) { parent.localRotation = Quaternion.Euler(Vector3.zero); parent.localScale = Vector3.one; parent = parent.parent; } } void Update() { Vector3 [] veces = GetBoxColliderVertexPositions(cube); for(int i =0; i< veces.Length; i ) { points[i].transform.position = veces[i]; } } Vector3[] GetBoxColliderVertexPositions (BoxCollider boxcollider) { var vertices = new Vector3[8]; //下面4个点 vertices[0] = boxcollider.transform.TransformPoint(boxcollider.center new Vector3(boxcollider.size.x, -boxcollider.size.y, boxcollider.size.z) * 0.5f); vertices[1] = boxcollider.transform.TransformPoint( boxcollider.center new Vector3(-boxcollider.size.x, -boxcollider.size.y, boxcollider.size.z) * 0.5f); vertices[2] = boxcollider.transform.TransformPoint( boxcollider.center new Vector3(-boxcollider.size.x, -boxcollider.size.y, -boxcollider.size.z) * 0.5f); vertices[3] = boxcollider.transform.TransformPoint(boxcollider.center new Vector3(boxcollider.size.x, -boxcollider.size.y, -boxcollider.size.z) * 0.5f); //上面4个点 vertices[4] = boxcollider.transform.TransformPoint(boxcollider.center new Vector3(boxcollider.size.x, boxcollider.size.y, boxcollider.size.z) * 0.5f); vertices[5] = boxcollider.transform.TransformPoint( boxcollider.center new Vector3(-boxcollider.size.x, boxcollider.size.y, boxcollider.size.z) * 0.5f); vertices[6] = boxcollider.transform.TransformPoint( boxcollider.center new Vector3(-boxcollider.size.x, boxcollider.size.y, -boxcollider.size.z) * 0.5f); vertices[7] = boxcollider.transform.TransformPoint(boxcollider.center new Vector3(boxcollider.size.x, boxcollider.size.y, -boxcollider.size.z) * 0.5f); return vertices; } }