Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Using hit.normal w/ FromToRotation forcing Y to always be 0

Discussion in 'Scripting' started by littlelingo, Dec 10, 2009.

  1. littlelingo

    littlelingo

    Joined:
    Jul 18, 2006
    Posts:
    372
    I implemented a terrain follow script and am having difficulty in setting the GO on the Y-axis to anything other than 0 as the script always sets the Y-axis to 0. The script looks like this:

    Code (csharp):
    1. var fwd:Vector3 = transform.TransformDirection(Vector3.forward);
    2.        
    3. transform.position += fwd/speedModifier;
    4.        
    5. var hit:RaycastHit;
    6. if (Physics.Raycast(transform.position, -transform.up, hit, 10)){
    7.        
    8.     if ( hit.distance > 1 ){
    9.         transform.position.y -= hit.distance-1;
    10.     } else if ( hit.distance < 1 ){
    11.         transform.position.y += 1-hit.distance;
    12.     }
    13.            
    14.     var rot:Quaternion = Quaternion.FromToRotation(Vector3.up, hit.normal);
    15.     transform.rotation = rot;
    16.            
    17.         }
    18.  
    Essentially no matter what I try, on the line where the transform.rotation gets set to rot, the Y-axis will either snap to 0, will bob violently or just will not work with some other undesirable result.

    Some examples of what I tried are:

    • - in getting the Quaternion, I changed Vector3.up to transform.up
      - in the line where I set the rotation to rot, I changed it to this transform.rotation *= rot
      - myriad of other random thing

    Any help here would be greatly appreciated as I am real stuck here. Thanks in advance!
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    If you want to keep the object floating a fixed height over the terrain, try something like:-
    Code (csharp):
    1. transform.position = hit.point + transform.up * floatHeight;
    Also, do this after you've set the rotation to the surface normal.
     
  3. littlelingo

    littlelingo

    Joined:
    Jul 18, 2006
    Posts:
    372
    @andeeee - thank you for the reply. I made the change you suggested ( makes sense ). :)


    Unfortunately, the Y-axis is still forcing the Y-axis to 0 so the GO is going in a single direction. The updated code looks like this:

    Code (csharp):
    1. var fwd:Vector3 = transform.TransformDirection(Vector3.forward);
    2.        
    3. transform.position += fwd/speedModifier;
    4.        
    5. var hit:RaycastHit;
    6. if (Physics.Raycast(transform.position, -transform.up, hit, 10)){
    7.  
    8. var rot:Quaternion = Quaternion.FromToRotation(Vector3.up, hit.normal);
    9. transform.rotation = rot;
    10.            
    11. transform.position = hit.point + transform.up * .5;
    12.  
    13.  
    I have attached a couple images for reference. Basically, the ant is rotated 180 on the Y-axis and the setting of the rotation to the FromToRotation is always forcing the Y-axis to 0. What I would like is that I can enforce the direction the ant is facing.

    Any ideas, I am hoping someone can help because I am stuck and yanking out all the hair I have left. :p

    Thank you in advance.
     

    Attached Files: