Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Rotating an object along the terrains normal, whilst looking at a position

Discussion in 'Scripting' started by MarkusDavey, Jun 18, 2011.

  1. MarkusDavey

    MarkusDavey

    Joined:
    Jun 3, 2011
    Posts:
    258
    Hello folks.

    I'm working on a vehicle, and I would like it to be able to drive along slopes whilst also looking at the position it has been told to move towards. How would i go about this? Rotation is a real big weak point for me.

    Cheers.
     
  2. MarkusDavey

    MarkusDavey

    Joined:
    Jun 3, 2011
    Posts:
    258
    bump :(


    A lovely diagram :)

    A: The problem

    B: The desired result.
     
    Last edited: Jun 18, 2011
  3. Todilo

    Todilo

    Joined:
    Feb 1, 2011
    Posts:
    88
    Simplest solution is transform.LookAt(your velocity-vector). this will make your gameobject looking uphill. This might cause jitterering, and the facing might need to be dampened/smooth depending on your environment.

    btw: If you use rigidbodies to move the car, well then it should actually already do this since it would be elevated by the terrain, and follow it.
     
  4. MarkusDavey

    MarkusDavey

    Joined:
    Jun 3, 2011
    Posts:
    258
    How do I get the velocity? in terms of it as a vector 3. as this in my movement system atm.

     
  5. Todilo

    Todilo

    Joined:
    Feb 1, 2011
    Posts:
    88
    You must have some sort of code that lifts the gameobject somewhere? because that line would actually move it inside the terrain. Have you tried using forces instead for moving the car. I think that is a very frequent way since well, there are a lot of in-built functions in the physics library that is helpful for cardriving. You could look at the unity-car-game tutorial.
     
  6. MarkusDavey

    MarkusDavey

    Joined:
    Jun 3, 2011
    Posts:
    258
    Well, it's an RTS unit, and i tried using rigids, but they behaved poorly. And there is a 'keeponground' function.
     
  7. MarkusDavey

    MarkusDavey

    Joined:
    Jun 3, 2011
    Posts:
    258
    which works by raycasting down and setting the object's Y position to the point it hits + bounds extents y
     
  8. Todilo

    Todilo

    Joined:
    Feb 1, 2011
    Posts:
    88
  9. MarkusDavey

    MarkusDavey

    Joined:
    Jun 3, 2011
    Posts:
    258
    Not like I would know what that means :p

    I also have no idea how to implement that line you suggested. The decs on it leaves much to be desired.
     
    Last edited: Jun 18, 2011
  10. Todilo

    Todilo

    Joined:
    Feb 1, 2011
    Posts:
    88
    ok well here is a script I found on unity-answers:
    Code (csharp):
    1.  
    2. // after raycast, or however you get normal:
    3. // Compute angle to tilt with ground:
    4. Quaternion grndTilt = Quaternion.FromToRotation(Vector3.up, hit.normal);
    5. // Base angle is straight up, spun only on y:
    6. // "facing" is the script variable used for turning:
    7. transform.rotation = Quaternion.Euler(0,facing,0);
    8. // tilt to align with ground:  
    9. transform.rotation = grndTilt*transform.rotation;  
    10.  
    11. If makes kind of a cool snap when you land after a small jump. If you want smooth, replace transform.rotation with Quaternion wantRotation; (and blend with trans.rot however.)
    12.  
    13.  
    You can use the sam raycast that you use for the snapping.
     
  11. MarkusDavey

    MarkusDavey

    Joined:
    Jun 3, 2011
    Posts:
    258
    cool! Everything is working, with the exception that they're not facing the direction they're moving. Which I assume is because I don't know how to achieve the 'facing' variable.

    I have such poor maths skills, it's saddening.
     
  12. Todilo

    Todilo

    Joined:
    Feb 1, 2011
    Posts:
    88
    ok this is a dirty fix. Save previous position. then after you updated to your new position(before snapping it and rotating it) do:

    Vector3 direction= (newestPosition-oldestPosition).normalized; and then do transform.LookAt(transform.position+direction); this would face yoru object forward. Then apply the other code above which will rotate it to follow path. I give no guarantee, my vector-math isnt that perfect and well, I am very tired.