我有一个imageTarget,我用ARCamera跟踪了该imageTarget,我想找到imageTarget相对于ARCamera(移动设备)的角度X,因为我需要将我的object(imageTarget)移向相机。我在Unity中使用Vuforia。如果可能的话,请提供给我代码示例,因为我是Unity新手,所以了解的不多。我在这里与您分享我的努力。我正在使用此代码计算角度,我认为这是不正确的。请检查一下。请帮我。
Transform dummy = new GameObject("Dummy").transform;
dummy.SetParent(trackableEventHandler.transform);
dummy.LookAt(Camera.main.transform, trackableEventHandler.transform.up);
float angle = Mathf.Atan2(dummy.forward.z, dummy.forward.x) - Mathf.Atan2(trackableEventHandler.transform.forward.z, trackableEventHandler.transform.forward.x);
angle *= Mathf.Rad2Deg;
text.text = angle.ToString();
答案 0 :(得分:0)
尝试一下
Transform dummy = new GameObject("Dummy").transform;
dummy.SetParent(trackableEventHandler.transform);
dummy.LookAt(Camera.main.transform, trackableEventHandler.transform.up);
float angle = Vector3.Angle(Camera.main.transform.forward, dummy.position - Camera.main.transform.position);
text.text = angle.ToString();
编辑:对于正负号,请改用此
float angle = Vector3.SignedAngle(Camera.main.transform.forward, dummy.position - Camera.main.transform.position, Vector3.up);
对于2D签名角,请改用此
float angle = Vector2.SignedAngle(Vector2(Camera.main.transform.forward.x, Camera.main.transform.forward.z), Vector2(dummy.positionx.x - Camera.main.transform.position.x, dummy.positionx.z - Camera.main.transform.position.z));