相机的前向矢量与从相机位置到原点的矢量之间围绕 X 和 Y 的度数
Degrees around X and Y between a camera's forward vector and a vector from the camera's position to the origin
我可以执行以下操作以获得我使用相机观察的位置和原点之间的整体 angle
:
var targetDirection = Vector3.zero - Camera.main.transform.position;
var angle = Vector3.Angle(targetDirection, Camera.main.transform.forward);
但是我怎样才能得到构成这个角度的绕 X 轴的度数和绕 Y 轴的度数呢?
我用平面投影算出来的:
var targetDirection = Vector3.zero /* Note: Can replace Vector3.zero with another target. */ - Camera.main.transform.position;
var horizontalPlaneNormal = Vector3.Cross(targetDirection, Vector3.right);
var vectorX = Vector3.ProjectOnPlane(Camera.main.transform.forward, horizontalPlaneNormal);
var angleX = Vector3.SignedAngle(targetDirection, vectorX, horizontalPlaneNormal);
var verticalPlaneNormal = Vector3.Cross(targetDirection, Vector3.up);
var vectorY = Vector3.ProjectOnPlane(Camera.main.transform.forward, verticalPlaneNormal);
var angleY = Vector3.SignedAngle(targetDirection, vectorY, verticalPlaneNormal);
Debug.LogFormat("AngleX: {0}, AngleY: {1}", angleX, angleY);
说明:
摄像头可以看任何地方,也可以放在任何地方。要获得准确的角度,您需要将相机所看位置的矢量投影到平面上。要构造您需要的水平面或垂直面,您需要使用叉积来获取其法线,以便使用 ProjectOnPlane()
方法。最后,您可以获得两个向量之间的有符号角度(您的目标方向向量和相对于相机所看位置的投影向量)。代码不言自明。干杯!
我可以执行以下操作以获得我使用相机观察的位置和原点之间的整体 angle
:
var targetDirection = Vector3.zero - Camera.main.transform.position;
var angle = Vector3.Angle(targetDirection, Camera.main.transform.forward);
但是我怎样才能得到构成这个角度的绕 X 轴的度数和绕 Y 轴的度数呢?
我用平面投影算出来的:
var targetDirection = Vector3.zero /* Note: Can replace Vector3.zero with another target. */ - Camera.main.transform.position;
var horizontalPlaneNormal = Vector3.Cross(targetDirection, Vector3.right);
var vectorX = Vector3.ProjectOnPlane(Camera.main.transform.forward, horizontalPlaneNormal);
var angleX = Vector3.SignedAngle(targetDirection, vectorX, horizontalPlaneNormal);
var verticalPlaneNormal = Vector3.Cross(targetDirection, Vector3.up);
var vectorY = Vector3.ProjectOnPlane(Camera.main.transform.forward, verticalPlaneNormal);
var angleY = Vector3.SignedAngle(targetDirection, vectorY, verticalPlaneNormal);
Debug.LogFormat("AngleX: {0}, AngleY: {1}", angleX, angleY);
说明:
摄像头可以看任何地方,也可以放在任何地方。要获得准确的角度,您需要将相机所看位置的矢量投影到平面上。要构造您需要的水平面或垂直面,您需要使用叉积来获取其法线,以便使用 ProjectOnPlane()
方法。最后,您可以获得两个向量之间的有符号角度(您的目标方向向量和相对于相机所看位置的投影向量)。代码不言自明。干杯!