I have an gun which raycasts onto a plane and I want to find the angle between the plane and the object based on the normal of the raycast hit. Also, I want to isolate the pitch and yaw seperately.
Currently I use this approach to find the pitch and yaw separately but it is not relative to the normal.
Vector3 targetDir=raycastHitPos - gunNozzle.transform.position;
float pitchAngle = Vector3.SignedAngle(new Vector3(0, targetDir.y, 0),gunNozzle.transform.forward, gunNozzle.transform.right);
float yawAngle = Vector3.SignedAngle(new Vector3(targetDir.x, 0, 0), gunNozzle.transform.forward, gunNozzle.transform.up);
Vector3 targetDir = Vector3.Cross(gunNozzle.transform.forward.normalized,normal.normalized);
but it doesn’t work :/.
How do I fit the raycast hitpoint normal into this code?
Fairly new to Vector math so please guide me on how to achieve this!
This image shows my current implementation and what I need
The yellow arrow is the facing direction and the green arrow is the normal of the red plane. These are the values which I get when facing exactly opposite to the normal. The second image shows the values which I want. I want the same angles whichever way I face the plane(or the plane is facing). The values are only for representation purposes, I’m fine with any range!
I’ve also tried
Reflecting the vector and then trying to find the angle between the hitpoint and the reflected vector but it only gives a value of either 180 or 0.
I also want to use Vector3.SingedAngle() because I want negative values!
I just did the same to create a “compass”. I used the function Vector3.Angle , you need two vectors, and you get the angle between them. Think about which vector defines your plane (0,1,1), or (1,0,1) or (1,1,0). Check the API and sure you will get it!
If helped, accept the answer! :D
The cross operation you did will always result in a third vector that is perpendicular to both operand vectors and parallel to the plane surface which will later play a big role deciding the sign of the angle if you used it as a third parameter inside SignedAngle method as following :
Vector3.SignedAngle(objectForwardVector, planeNormal, crossResult);
So if we applied that on your example image then you will get the signed angle between the green and yellow arrows which will be 180 or -180 degrees (depends on the direction of the cross product result) and will never be 90, but at least you will get a result that is always relative to the plane normal.
I Found the solution, it’s only for pitch currently, will update it once I get the Yaw sorted out!
I figured it would be easier to find the angle between the normal and the gun’s nozzle but with only y and z values (since I didn’t want to take yaw into account) and surprisingly, it works!
I shall accept this as an answer and will get back if i face problems with yaw
This is the code for the pitch only angle
float pitchAngle = Vector3.SignedAngle(normal, new Vector3(0, gunNozzle.transform.forward.y, gunNozzle.transform.forward.z), gunNozzle.transform.right);
Thank you for everyone who answered, commented and saw this question!