I have the red object moving in the black arrow direction. I want the green object to move in the blue arrow direction in response to the red objects movement. Now I know how to move things, all I need is purely the most clean way to find the vector for the blue arrow movement.
On the 1st image its pretty simple, black vector is (1,0) - which makes the blue vector (0,-1)
I can add an extra check to see if the green object is below the red one and position 2 is solved as the blue arrow is (0,1) for direction
Now this is brute forcing the logic and it gets messy fast once the source direction can be anywhere in 360º.
I can surmise that the core of what I need is to create a direction vector that points perpendicular to the original one, in the direction of the green object relative to the red one;
But mathematically what are the steps I need to achieve this? Perhaps something to do with angles?
For this example lets assume that the green object is never perfectly centered on the line of movement ( I can solve that later)
thanks! What is the best way to detect which side the vector should point? In other words how should I compare the position of the red object to the green one and the source vector to know if I should multiply it by -1 or not?
Not sure I get it from your screenshot. You want to basically create a line at the center of your red object position, following its directional vector. Anything to the relative left goes left, anything to the relative right goes right?
Get the directional vector from the red to the green and normalize it. Something like Vector2 a = (greenobject.position - redobject.position).normalized;
Then get the vector perpendicular to your red object and normalize that as well, if not already.
Vector2 b = Vector2.Perpendicular(redobjectvector).normalized;
Then you can use the dot product to measure the alignment between 2 vectors. So basically you compare the red to green direction to the perpendicular vector. If the output value is greater than 0, then this is the correct direction, if it’s negative, then the opposite is correct.