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

Question How would you go about rotating an object so that it is exactly perpendicular to a wall?

Discussion in 'Scripting' started by Proxyuto, Aug 2, 2022.

  1. Proxyuto

    Proxyuto

    Joined:
    Aug 2, 2022
    Posts:
    5
    I wasn't sure how to condense the question into a good title but the image I attached should do a better job at explaining what I'm looking for. I'm having trouble getting a rotation script to work like I have in mind and I'm not sure if I'm missing something super obvious.

    Here is what I would like the script to do:
    1. A Raycast is shot from the front of the Arrow object (The arrow in the picture).
    2. If the Raycast intersected with another object that is set to the 'Wall' layer, rotate the Arrow object so that it is perpendicular to the wall that the Raycast hit.

    The following is what I have written so far, only problem is that the Arrow object would end up facing towards the point where the Raycast intersected the wall, rather than having the Arrow rotate to face perpendicular to the wall. Any ideas?

    Code (CSharp):
    1.     public void FaceTowardsWall(RaycastHit hit)
    2.     {
    3.         Vector3 targetDirection = Vector3.zero;
    4.  
    5.         targetDirection = hit.point - transform.position;
    6.  
    7.         //Set the rotation. Throw away any rotation that makes the object rotate UP or DOWN
    8.         transform.rotation = Quaternion.LookRotation((new Vector3(targetDirection.x, 0, targetDirection.z)));
    9.     }
     

    Attached Files:

    MinhocaNice likes this.
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    This should do the trick:
    Code (CSharp):
    1. transform.rotation = Quaternion.LookRotation(-hit.normal, transform.up);
     
  3. Proxyuto

    Proxyuto

    Joined:
    Aug 2, 2022
    Posts:
    5
    Huh... it was THAT simple. I didn't even notice LookRotation accepted a second parameter.

    I'm still a little confused as to why it works though. If a normal only returns a Vector3 whose 3 values range from 0 to 1, why is it that our Arrow Object doesn't end up rotating towards the center of the world with a slight offset based on the given normal? I was under the impression that the only way to use LookRotation was to subtract the current position from the target position and input the result of that into the LookRotation. Yet, as far as I know, this answer just gives the target position.

    Regardless, thank you very much for the reply! Quaternions/rotation sometimes snap my brain in two so this helps a ton.:D
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    The normal is not a position vector, it's a direction vector.

    LookRotation doesn't look at a position vector. It looks along a direction vector. So if you pretend your arrow is the center of the world, then add the direction vector to it, that would be the actual point you're looking at.
     
  5. Proxyuto

    Proxyuto

    Joined:
    Aug 2, 2022
    Posts:
    5
    Oooh~ I see. That distinction between a position vector and a direction vector was what I was missing. Thank you for taking the time to explain!