I’m not sure that what I wrote in the title is explicit enough so I’ll explain with a sketch.
I want to get the angle between 2 vectors.
Here’s what i get
But I need to differentiate the two angle according to their orientation and basicl get something like that :
I know that the point of using radians but I can’t find a way to get the angle between two vectors in rad.
And of course just converting them with the mathf.deg2Rag const won’t work.
Any idea ? I’m open to any suggestion to solve or to get around the problem.
If you’re just getting out a float value, I can’t see any meaningful way to differentiate them such that a = -b. As a description of the relation between two line segments, a and b are the same thing. As a shape in 2D space, you can’t describe their difference with simply a float.
Do you have some examples of what you want your output to be given some specific inputs? I’m struggling to see what you’re looking for.
Yes, the two green lines are the vectors, forgot the tip of the arrow…
Well to explain simply and into context :
I have a door and i’m trying to determine where the player is to that door (front left, front right etc…)
I wanted to do this according the the angle. Here’s another sketch :
therefore I can determine that :
If 0 < a < 90 : The player is in front to the left
if 90< a <180 The player is behind to the left
etc…
I need to know this 'cause I’m trying to get a realtime door opening system (same’s as amnesia’s)
The way I did the opening is not realy convenient nor efficient because I use the Rigidbody AddTorque function.
So I need to know the angle between the player and the door to get it react logicly to users inputs.
I’m sorry if i’m not realy clear, English is not my mother tongue
You can use the cross product to get this information. By definition, the cross product of two vectors is a third vector orthogonal to both input vectors. If you flip the input vectors, the cross product is negated. The cross product can also be used to determine in which direction to rotate (clockwise or counter-clockwise) and by what angle, considering the shortest rotation, so that both input vectors are aligned.
You can do the similar computation with the door right vector as well. dot(playerDirection, door.transform.right) will be positive if the player is on the right and negative if on the left.
Note that player direction is relative to the door:
ericbegue has already given you everything you need to figure this out. The player direction and the DOT product.
Vector3 playerDirection = (player.transform.position - door.transform.position).normalized;
float frontDot = Vector3.Dot(playerDirection,Door.transform.forward);
float rightDot = Vector3.Dot(playerDirection,Door.transform.right);
if(frontDot>0)
{
//player is in front of door
}
else if(frontDot<0)
{
//player is behind door
}
else
{
// player is neither in front or behind,
// but along the same plane as the door's front
}
if(rightDot>0)
{
//player is right of door
}
else if(rightDot<0)
{
//player is left of door
}
else
{
// player is neither to the left or right
}