- Home /
How does one find the position to the left or right of an object based on the position of another?
Hello all,
I have a vessel moving towards another object at a random angle, and have an obstacle in the way at a random rotation, all in 2D space. How would I go about calculating the positions marked in red in the diagram linked below, based on the axes of the obstacle and the movement direction of the vessel?
Would I be right in thinking that a good starting point would be to get the vector from the obstacle to the vessel moving towards it by doing the following, and then creating an offset from that?
Vector2 vesselDirFromObstacle = vessel.transform.position - obstacle.transform.position;
Please see attached diagram for further information.

Answer by edcx · Dec 13, 2016 at 08:15 AM
Well, this looks like a collision avoidance problem to me. Also, you have the dir vector to the obstacle. So you can rotate that vector around z axis to get left and right vectors.
Vector3 forward = vessel.transform.position - obstacle.transform.position;
Vector3 left = Quaternion.AngleAxis(90, Vector3.forward) * forward.normalized;
Vector3 right = Quaternion.AngleAxis(-90, Vector3.forward) * forward.normalized;
Or you can use some trigonometry to calculate.
Hope this helps.
This is exactly what I've been looking for, thank you. I'd like to select this as the answer, but I'm unable to.
Now an answer, so you can accept the solution.
Answer by RobAnthem · Dec 09, 2016 at 08:05 PM
The Unity manual covers it pretty well Here
var distance = heading.magnitude;
var direction = heading / distance; // This is now the normalized direction.
Thanks, but not quite what I'm after. Your link shows something I'm already able to do, and as stated above, I can get the vector from the obstacle to the vessel.
What I'm after is getting the up or down vector from the one that points to the vessel.
Example: If the vector pointing at the vessel from the obstacle appears to be at an angle of 225 degrees visually, I'd be looking to get the up vector which would be at an angle of 315 degrees visually.
Your answer
