下面代码是用在HoloLens中的,菜单View 始终跟随用户,在用户头顶显示,并且可以点击菜单任意位置。
本身不难,仅仅是为了一次编写多次拷贝。欢迎拷贝。
void Update(){
var camtra = CameraCache.Main.transform;
var pos = camtra.position +
new Vector3(camtra.forward.x * 1.5f, 0.6f, camtra.forward.z * 1.5f);
if (Vector3.Distance(pos, view.transform.position) > 0.2f)//跟随距离
{
view.transform.position = Vector3.Lerp(view.transform.position, pos, 0.2f);
}
else
{
var pos1 = view.transform.position - camtra.position;
var pos2 = camtra.forward;
pos1.y = 0;
pos2.y = 0;
var ang = Vector3.Angle(pos1, pos2);
if (ang > 30)//可活动角度
{
view.transform.position = Vector3.Lerp(view.transform.position, pos, 0.2f);
}
}
view.transform.rotation = Quaternion.LookRotation(view.transform.position - camtra.position);
}