角色低头抬头,始终保持朝着自身物体正前方向
public class Test : MonoBehaviour
{
Vector3 v33;
// Start is called before the first frame update
void Start()
{
if (transform.position.z==0)
{
v33 = -Vector3.Cross(transform.right, Vector3.up);
}
else
{
Vector3 v3 = Vector3.Cross(transform.forward, Vector3.up);
//v3.Normalize();
v33 = -Vector3.Cross(v3, Vector3.up);
}
}
// Update is called once per frame
void Update()
{
//transform.Translate(0,0,1/*,Space.World*/);
transform.Translate(v33*Time.deltaTime,Space.World);
}
}
向左向右,计算旋转角度
var v = uiObjs["Camera (eye)"].transform.position - uiObjs["SK_BaoAn"].transform.position;//方
//v.y = 0;
var angle = Mathf.Acos(Vector3.Dot(v.normalized, uiObjs["SK_BaoAn"].transform.forward)) * Mathf.Rad2Deg;//计算转的角度
var s = Vector3.Cross(v, uiObjs["SK_BaoAn"].transform.forward);//向左转向右转
angle *= s.y < 0 ? 1 : -1;
Debug.Log(angle);
uiObjs["SK_BaoAn"].transform.DORotate(new Vector3(0,angle,0), 1f);
物体正前方,平滑转动
public void fn_showUI(Transform tf)
{
Vector3 v3_1 = Vector3.Cross(tf.forward, Vector3.up);
Vector3 v3_2 = Vector3.Cross(v3_1, Vector3.up).normalized;
if (distance != 0) transform.position = tf.position - v3_2 * distance;
transform.forward = -v3_2;
//transform.position = Vector3.Lerp(transform.position, tf.position - v3_2 * distance,0.2f);
}
地图小图标跟随VR相机视角角度
private void Update() {
Vector3 v3 = Vector3.Cross(eyes.forward, Vector3.up);
v33 = Vector3.Cross(v3, Vector3.up);
float dot = Vector3.Angle(-v33, Vector3.forward);//转的角度
float dd = Vector3.Dot(-v33, Vector3.right);//判断向左向右
if (dd < 0) {
Debug.Log("向左"+ dot);
point.eulerAngles = new Vector3(0, 0, dot);
}
else {
Debug.Log("向右" + dot);
point.eulerAngles = new Vector3(0, 0, -dot);
}
}