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

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.

bump :frowning:


A lovely diagram :slight_smile:

A: The problem

B: The desired result.

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.

How do I get the velocity? in terms of it as a vector 3. as this in my movement system atm.

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.

Well, it’s an RTS unit, and i tried using rigids, but they behaved poorly. And there is a ‘keeponground’ function.

which works by raycasting down and setting the object’s Y position to the point it hits + bounds extents y

Perhaps the Locomotion system could be of help. It handles uneven/angled terrain:

Propbably all you need though is this:
TerrainData.GetInterpolatedNormal

Remember that the values are normalized.

Not like I would know what that means :stuck_out_tongue:

I also have no idea how to implement that line you suggested. The decs on it leaves much to be desired.

ok well here is a script I found on unity-answers:

// after raycast, or however you get normal:
// Compute angle to tilt with ground:
Quaternion grndTilt = Quaternion.FromToRotation(Vector3.up, hit.normal);
// Base angle is straight up, spun only on y:
// "facing" is the script variable used for turning:
transform.rotation = Quaternion.Euler(0,facing,0);
// tilt to align with ground:   
transform.rotation = grndTilt*transform.rotation;   

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.)

You can use the sam raycast that you use for the snapping.

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.

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.