赞
踩
1、设置rotation
此方法的基础是将对象绑定在场景中的一个对象上,然后设置对象的rotation或者lookat,需要在Update中时时更新
- //方法1
- //m_Name.transform.LookAt(Camera.main.transform.position);
- //m_Name.transform.rotation = Quaternion.Slerp(Quaternion.LookRotation(Camera.main.transform.position - m_Name.transform.position), m_Name.transform.rotation, 10 * Time.deltaTime);
- //方法2
- //Vector3 v = Camera.main.transform.forward;
- //m_Name.transform.forward = v;
- //m_Name.transform.forward = Vector3.Slerp (m_Name.transform.forward,v,10*Time.deltaTime);
- //方法3
- lastRotation = m_cameraTransform.rotation;
- m_Name.transform.rotation = lastRotation;
上面列出了3中设置方法,对象的初始化方法如下:
- headShowObj = new GameObject("playerName");
- objTrans = headShowObj.transform;
- objTrans.parent = _controller.trans;// Camera.main.transform;
- objTrans.localPosition = Vector3.zero + v;
- objTrans.localRotation = Quaternion.identity;
- objTrans.localScale = Vector3.one;
- objTrans.LookAt(Camera.main.transform.position);
- m_Name = headShowObj.AddComponent<TextMeshPro>();
- m_Name.font = Resources.Load ("UI/Fonts/SIMLI",typeof(TextMeshProFont)) as TextMeshProFont;
- m_Name.fontSize = 4;
- m_Name.isOverlay = true;
- m_Name.alignment = AlignmentTypes.Center;
- m_Name.anchor = AnchorPositions.Center;
2、设置position
此方法的基础是在对象初始化的时候设置对象的rotation或者lookat,然后在Update中更新对象的position
初始化方法:
- headShowObj = new GameObject("playerName");
- objTrans = headShowObj.transform;
- objTrans.parent = _controller.trans;// Camera.main.transform;
- objTrans.localPosition = _controller.trans.localPosition + v;
- objTrans.localRotation = Quaternion.identity;
- objTrans.localScale = Vector3.one;
- objTrans.LookAt(Camera.main.transform.position);
- m_Name = headShowObj.AddComponent<TextMeshPro>();
- m_Name.font = Resources.Load ("UI/Fonts/SIMLI",typeof(TextMeshProFont)) as TextMeshProFont;
- m_Name.fontSize = 4;
- m_Name.isOverlay = true;
- m_Name.alignment = AlignmentTypes.Center;
- m_Name.anchor = AnchorPositions.Center;
- lastRotation = m_cameraTransform.rotation;
- m_Name.transform.rotation = lastRotation;
但是,有一个想法:加入我在初始化时就设置对象的rotation为面向摄像机,而且把它绑定在场景中的父对象,这样就不用updateposition和rotation了,但是试了下,当父对象rotation时,子对象也跟着rotation,所以,rotation或者position是必须有一个是update的。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。