Search Unity

Aligning object to surface normal with Quaternion.LookRotation

Discussion in 'Scripting' started by bigTimeOperator, Oct 12, 2010.

  1. bigTimeOperator

    bigTimeOperator

    Joined:
    Nov 16, 2007
    Posts:
    24
    I have a cube and a terrain object. I'm trying to cause the cube to orient itself properly with the terrain. For now I have my cube floating above the terrain and I'm casting a ray downwards onto the terrain and trying to set the orientation of the rigid body based on the RaycastHit normal. As I move the game object over the terrain the object seems to be rotating around the z axis properly, but I can understand why the X axis isn't effected.


    Here is the code I have attached to the cube:

    Code (csharp):
    1. function update() {
    2.  
    3.     var hit1: RaycastHit;
    4.    
    5.     if (Physics.Raycast(transform.position,-Vector3.up,hit1)) {
    6.             var objectForward : Vector3 = transform.TransformDirection(Vector3.forward);
    7.             transform.rotation = Quaternion.LookRotation (objectForward, hit1.normal);
    8.             //transform.position = hit1.point;
    9.     }
    10. }
    11.  
    And here is the code with the debugging.. if it helps.

    Code (csharp):
    1. function update() {
    2.     var hit1: RaycastHit;
    3.    
    4.     Debug.DrawRay(transform.position,-Vector3.up * 100,Color.red);
    5.    
    6.     if (Physics.Raycast(transform.position,-Vector3.up,hit1)) {
    7.             Debug.DrawRay(hit1.point,hit1.normal * 100,Color.blue);
    8.             var objectForward : Vector3 = transform.TransformDirection(Vector3.forward);
    9.             Debug.DrawRay(transform.position, objectForward * 100,Color.yellow);
    10.             Debug.DrawRay(hit1.point,hit1.normal * 100,Color.green);
    11.             transform.rotation = Quaternion.LookRotation (objectForward, hit1.normal);
    12.             //transform.position = hit1.point;
    13.     }
    14. }
    15.  
     

    Attached Files:

  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Quaternion.LookRotation aligns the object's Z direction exactly to the forward (the first parameter) and then rotates it around that vector axis until it its Y direction is aligned as closely as possible with the supplied upward direction (the second parameter). In other words, of the two vectors, the first gets priority when they can't both be aligned exactly. You probably want to look at Quaternion.FromToRotation to align the upward direction to a surface normal. The manual page for the function gives an example of how to do this.
     
    James3421 and harko12 like this.
  3. bigTimeOperator

    bigTimeOperator

    Joined:
    Nov 16, 2007
    Posts:
    24
    Thank you so much. I am having luck with FromToRotation by setting the rotation to Quaternion.FromToRotation(Vector3.up,hit.normal). The script reference doesn't really explain whats going one, but I assume this is just taking the Objects up orientation and rotating to match the normal of the surface?

    I'm encountering another problem now though - The cube maintains a constant y axis orientation.. or for a lack of a better word it's always pointing straight forward. Ideally I'd like to set the y rotation and then have it align to the surface normal in both x,z rotations but maintain the y axis which will be controlled by user input. I was hoping it would be as simple as setting the object to align to the surface and then just rotating it around it's own y axis. When I do that however I can see that the local transform up vector is wobbling off of the hit.normal vector.

    I can't quite wrap my head around why that would be occurring, but I know this is a fairly common thing to need to do. I have been digging through other forum topics and I see other people mentioning use of LookRotation method in this situtation, but I that just brings me back to my original post.
     
  4. bigTimeOperator

    bigTimeOperator

    Joined:
    Nov 16, 2007
    Posts:
    24
  5. Ridgerunner

    Ridgerunner

    Joined:
    May 31, 2014
    Posts:
    34
    Hi I'm working some similar to what you did you ever get it working and what did you do to get it working
    Your help would be very appreciated thanks
     
  6. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    The thread was last updated a little less than 7 years ago. If you click the name of the person you're asking that question, you'd see that "bigTimeOperator was last seen: May 2, 2011"


    What I'm saying here is that you'll probably not get help in this way. Check the dates on threads you find on google.
     
    MachoBrizzin and DDeathlonger like this.