Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Getting perpendicular direction vector from surface normal

Discussion in 'Scripting' started by sysameca, Jun 6, 2014.

  1. sysameca

    sysameca

    Joined:
    Mar 2, 2013
    Posts:
    99
    Hello guys. My problem which i can't solve is this: I want to find out the perpendicular direction of a normal (the red arrow) from the surface normal (green line) which must always point at the direction of the ray (light purple color).Just to clarify: i know how to find the perpendicular of the normal, but i don't know how to get the correct perpendicular in order to achieve the result from the image.
     

    Attached Files:

  2. tomvds

    tomvds

    Joined:
    Oct 10, 2008
    Posts:
    1,028
    You can project the direction of the ray onto the normal using Vector3.Project. Subtract the resulting vector from the ray direction to get the direction vector you're looking for. Note that the result may be Vector3.zero if your ray is aligned with the normal.
     
  3. sysameca

    sysameca

    Joined:
    Mar 2, 2013
    Posts:
    99
    I graphed your answer and it's not even close to the result i want to achieve.Tried it also in Unity to be sure i don't mistake something bit i get super wrong results.Maybe somebody can take a closer look and give me a correct answer.Let me clarify again: I want to get the correct perpendicular direction (red arrow) of the normal (N) depending on the ray which is casted.
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Your question isn't really clear. What do you consider to be the "correct perpendicular vector"? Your drawing looks like the cross product of the normal and the width of the collider (or the depth of it I guess if you raycasted the shorter side).

    Edit: I should say the vector representing the center of the collider towards the extents of the collider to be more specific I suppose...
     
  5. sysameca

    sysameca

    Joined:
    Mar 2, 2013
    Posts:
    99
    Yes i suppose i didn't put the question completely clear.Anyway i got the solution from Unity answers so ill post it here just in case somebody needs it and I will explain what does it do.
    In my case I needed to get the perpendicular vector of the normal (N) so it points at the same direction as the ray's direction.
    My question could be put simply like: for instance the perpendicular of the hit normal is: Vector3(-hit.normal.y, hit.normal.x) * -1; or Vector3(-hit.normal.y, hit.normal.x); I want to find which one of those should it be used, depending on the casted ray's direction.I hope this was more clear..
    So the answer is:
    Code (CSharp):
    1. Vector3 surfaceParallel = direction - normal * Vector3.Dot(direction, normal);
     
    pepperopigeon likes this.