- Home /
Off Screen direction indicator HUD element / wrestling with Quaternion
Hi I'm attempting to create a HUD element that appears if the players target has left the screen,its fixed to the edges of the screen and helps inform the player which way to turn to get them in the view again. my game has full 3d movement so it has to work with all possible rotations. I've almost got it working I'm just having trouble with constructing the quaternion that allows me easy access to the values I need.
My current quaternion doesn't seam to take the roll into account correctly as so doesn't output the correct vector
Vector2 Offscreenindicator(Transform me,Transform target)
{
//Construct a Quaternion that has x and y euler angles relative to the players roll
Quaternion test = Quaternion.LookRotation(target.position-me.position,-me.up)*me.rotation;
//make those euler angles easily accessable
Vector3 temp = test.eulerAngles;
float pitch = temp.x;
float yaw = temp.y;
//calculate the horizontal FOV
float Hfov = (2* Mathf.Atan(Mathf.Tan((pc.cam.fieldOfView * Mathf.Deg2Rad)/2)*pc.cam.aspect))*Mathf.Rad2Deg;
//normalise the angles to a 180 to -180 system
if(pitch > 180)
pitch -= 360;
if(yaw > 180)
yaw -= 360;
//clamp the angles to the edges of the screen then normalise them to 0 to there feild of view
pitch = Mathf.Clamp(pitch,-pc.cam.fieldOfView/2,pc.cam.fieldOfView/2)+pc.cam.fieldOfView/2;
yaw = Mathf.Clamp(yaw,-Hfov/2,Hfov/2)+Hfov/2;
//compare with the feild of view to convert to a screen locaton
pitch = (pitch / pc.cam.fieldOfView)*(Screen.height);
yaw = (yaw/Hfov)*Screen.width;
//return a value that can be used with GUI Components
return new Vector2(yaw,pitch);
}
Answer by Lord Simpson · Aug 08, 2011 at 10:59 PM
In the end replaced the whole idea with a pointer instead that is only a few cm [0.1] units big and floats a meter or so in front of the camera pointing to the target and moving towards it by a small degree. Also I deactivate the mesh render if the Vector3 angle between its pointing direction and the cameras forward vector are below 30 so it doesn't take up screen space when the target is obviously visible. If the target is directly behind you the pointer looks larger as its closer to the screen and points directly towards the camera.
Your answer
Follow this Question
Related Questions
How to position 3D-GUI-Mesh on change of aspect ratio? 0 Answers
Maintaining Image Aspect Ratios in GUI using Matrix? 1 Answer
layer 2 guis onto of another 2 Answers
Make my HUD full screen 2 Answers