- Home /
Trying to get the angle of a camera object based on players forward, is not distance independent
Hello, I'm trying to get the angle in degrees from -180 to 180 of my camera around my player object based on it's forward. Right now this code is very close to working, but it is not distance independent.
void CameraDirection()
{
forward = transform.TransformDirection(Vector3.forward);
cameraPos = (cam.transform.position - transform.position);
cameraPos = cameraPos.normalized;
forward = forward.normalized;
angle = Vector3.Angle(forward, cameraPos);
sign = Mathf.Sign(Vector3.Dot(Vector3.up, Vector3.Cross(forward, cameraPos)));
signedAngle = angle * sign;
}
The camera orbits the player, and when I cross the camera in front of my player's forward it will not cross -1 to 1 as I want it to but instead it crosses some arbitrary number based on the distance of the camera from the player. For example at the distance I have it orbiting now it will cross the forward at about -26 to 26. How can I make it so that my player's forward is counted as 0 degrees no matter how far away the camera is from the player? I'm sorry if I'm missing something simple here, I'm fairly new to vector math and unity in general. Thank you!
Your answer

Follow this Question
Related Questions
Quick Angles Question 2 Answers
Clamp rotation of sprite? 0 Answers
0-360 Y degree from Vector3.Angle 1 Answer
Vector3.SignedAngle wrong direction when crossing the 0 point 1 Answer