Search Unity

Resolved How to get perpendicular direction vector?

Discussion in 'Scripting' started by rarac, Oct 22, 2021.

  1. rarac

    rarac

    Joined:
    Feb 14, 2021
    Posts:
    570
    Given this image to illustrate what I'm trying to achieve:

    Untitled.png 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 in advance!
     
  2. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    994
    Vector2.Perpendicular
    Output vector * -1 to get the opposite(so rotated clockwise, instead of counter clockwise).
     
  3. rarac

    rarac

    Joined:
    Feb 14, 2021
    Posts:
    570
    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?
     
  4. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    994
    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?
     
  5. rarac

    rarac

    Joined:
    Feb 14, 2021
    Posts:
    570
    yes thats correct
     
  6. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    994
    Get the directional vector from the red to the green and normalize it. Something like
    Code (CSharp):
    1. Vector2 a = (greenobject.position - redobject.position).normalized;
    Then get the vector perpendicular to your red object and normalize that as well, if not already.
    Code (CSharp):
    1. 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.
    Code (CSharp):
    1. float dot = Vector2.Dot(a, b);
     
    rarac likes this.
  7. rarac

    rarac

    Joined:
    Feb 14, 2021
    Posts:
    570
    thanks a lot!