- Home /
How to calculate a Vector3 point having three given Vector3 points and an angle of 90 degrees?
Hello together!
I'm currently implementing an obstacle avoidance system. I have my main character (1), a small obstacle (2) and the desired destination (3).
Trying to move my character to the desired destination results in detecting the obstacle within its range. Now i want to send the obstacle away to make way for the character.
I am casting two rays (yellow) outside character's range (blue) with the additional range of the obstacle's radius.
I would like to start checking valid positions for the obstacle at point 4 - let's call it position x on the left ray. If this point isn't valid cause of another kind of obstacle at this place, I will check the same position on the other ray. If this position is false too, I would like to increase the position x to x+1 and check again.
To get a ray point I could easily make use of:
Ray ray;
ray.GetPoint(float distance);
But to start exactly at point 4 I need to get the distance from character's position to the white arrow on the green line somehow.
So how can I calculate this specific distance or at least the point where the white arrow is pointing to?
Answer by Zerot · Sep 17, 2012 at 11:43 AM
If I understand it correctly, you want the point which is the projected point of the obstacle point onto the green line.
Unity has a function for that: http://docs.unity3d.com/Documentation/ScriptReference/Vector3.Project.html
You could also calculate it yourself using the dotproduct.
Thank you for your reply.
This sounds quite interesting, but I don't know how to get it work. I've tried something like this:
// Start
Vector3 toDestination = characterDestination - characterPosition;
Vector3 toDestinationNormalized = Vector3.Normalize(toDestination);
Vector3 toObstacle = obstaclePosition - characterPosition;
Vector3 projectedPoint = characterPosition + toDestinationNormalized * Vector3.Dot(characterPosition, toObstacle);
Vector3 projectedPointUnity = Vector3.Project(toDestination, obstaclePosition);
// End
But the resulting point is definitely not on the straight direction line.
well, that is because you are using it incorrectly ;)
Vector3 projectedPointUnity = characterPosition + Vector3.Project(toObstacle, toDestinationNormalized);
This should give you the correct position(have not tested it, so might contain typos)
Yes, that would work but it seems you are still missing the value for toObstacle. I do not see where you get this one from.
Ok. I don't know why, but I've tried it yesterday and it didn't work and now I write the same code again and it does. There must have been a stupid little mistake in just a few code lines. Funny. So your solution works perfectly for me right now. Thank you both for helping me!