获取怪物,角色头顶血条位置
作者:互联网
bool FindGameObjectByMesh(Transform trans,ref Vector3 _center)
{
Renderer[] _randers = trans.GetComponentsInChildren<Renderer>();
if (_randers != null)
{
foreach (Renderer item in _randers)
{
_center += item.bounds.center;
}
_center /= trans.GetComponentsInChildren<Renderer>().Length;
Bounds bounds = new Bounds(_center, Vector3.zero);
foreach (Renderer child in _randers)
{
bounds.Encapsulate(child.bounds);
}
// Debug.Log(bounds.size);
_center = bounds.center - trans.position;
return true;
}
else
{
_center = Vector3.zero;
return false;
}
}
void SetPos()
{
if (GUILayout.Button("如果没有合适的血条节点就点击设置一个坐标点"))
{
GameObject modelObj = myMeshSlot.gameObject;
Vector3 temp = Vector3.zero;
if (FindGameObjectByMesh(modelObj.transform, ref temp))
{
GameObject _headUI = GameObject.Find(modelObj.name + "/headUIRoot");
if (_headUI != null)
{
DestroyImmediate(_headUI);
}
if (_headUI == null)
{
_headUI = new GameObject("headUIRoot");
_headUI.transform.parent = modelObj.transform;
}
_headUI.transform.position = new Vector3(0, temp.y * 2, 0);
//Debug.Log(_headUI.transform.localPosition);
myMeshSlot._headUIRootPos = _headUI.transform.position;
DestroyImmediate(_headUI);
}
}
}
标签:血条,center,头顶,Vector3,transform,bounds,randers,怪物,headUI 来源: https://blog.csdn.net/qq_32818935/article/details/100575088