I have a cube that looks like this:
my camera looks at the cube like this
The cube has faces F(front), L(left), B(back), R(right)
and the top face is U(up) and bottom face is D(down)
I need to get a value that represents the current rotation of the cube VS the direction the camera is looking at it
This code does what I want, as long as the cube never “tilts”
private float AngleFromVecToVec_IgnoringY(Vector3 cubeTransformForward, Vector3 CamTransformForward)
{
Vector3 cubeTransformForward_IgnoringY = new Vector3(cubeTransformForward.x, 0, cubeTransformForward.z);
Vector3 CamTransformForward_IgnoringY = new Vector3(CamTransformForward.x, 0, CamTransformForward.z);
return Vector3.Angle(cubeTransformForward_IgnoringY, CamTransformForward_IgnoringY) * Mathf.Sign(Vector3.Dot(Vector3.Cross(cubeTransformForward_IgnoringY.normalized, CamTransformForward_IgnoringY.normalized), Vector3.up));
}
This code gives 180 if the cube is full on facing the camera, 0 if its completely facing away, and signed angle values for degrees of rotation in between, it allows me to pinpoint exactly how much the cube is rotated on the Y axis
The issue comes now that the cube can now be TILTED and so I can no longer ignore the y component of the transform forward vectors like Ive previously been doing
I tried this:
private float AngleFromCubeForwardToCamDir(Vector3 cubeFwdVec, Vector3 cubeUpVec, Vector3 camDirVec)
{
//Vector3 partFwdVecNormalized = partFwdVec.normalized;
Vector3 cubeFwdVec_IgnoringYNormalized = new Vector3(cubeFwdVec.x, 0, cubeFwdVec.z).normalized;
Vector3 camDirVec_IgnoringYNormalized = new Vector3(camDirVec.x, 0, camDirVec.z).normalized;
Vector3 upwardsAlignment = cubeUpVec;
return Vector3.Angle(cubeFwdVec_IgnoringYNormalized, camDirVec_IgnoringYNormalized) * Mathf.Sign(Vector3.Dot(Vector3.Cross(cubeFwdVec_IgnoringYNormalized, camDirVec_IgnoringYNormalized), upwardsAlignment));
}
but this is obviously wrong, because now being rotated on the X axis will produce an angle that is indistinguishable from rotation on the Y axis
It also doesnt actually take into account the fact that the camera is looking at the cubes from a 45º perspective, which so far I have been ignoring by eliminating the Y component of the camera transform forward, so that is obviously wrong aswell
I dont know what to try anymore, can someone give me some kind of advice?