- Home /
follow terrain script works, but Slerp breaks it!
I have this script making my car follow the undulations of the track:
forward = Vector3.Cross(transform.right ,groundHit.normal );
transform.rotation = Quaternion.LookRotation(forward, groundHit.normal);
...which works great, but the alignment of the car to the track is obviously very snappy... so I tried this:
forward = Vector3.Cross(transform.right ,groundHit.normal );
targetRotation = Quaternion.LookRotation(forward, groundHit.normal);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 20);
which does indeed smooth it out, but it seems to be breaking the rotation it's trying to get to... the car keeps nose-diving into the floor every time the surface normal changes...
any ideas?
I assume the car is moving forward at the same time? What are you using for movement: character controller, rigidbody, directly changing the transform?
$$anonymous$$y visualization is that you are moving the car on it 'forward'. Since the rotation completes over time, the forward will be pointed into the surface during the time the car is realigning itself to the new surface.
using rigidbody AddRelativeForce to move the car forward...
what you've said makes sense actually... when you say the rotation completes over time, shouldn't it still be somewhere between transform.rotation & targetRotation, and hence not be pointing into the floor at any point!?
thanks for the input.
This is what I'm thinking. The red arrow is what you would get with an immediate change. The green arrow is the Slerp() in progress. $$anonymous$$otion is into the hill. I don't know enough about your code, but I'm guessing that you are applying force in the objects forward direction. Perhaps the force should be applied along the red arrow even as the object is facing the green arrow.
doh. still does the same :-( It seems a little better, but still nose-dives after moving foward a few meters...
here's the whole script, in case it helps:
function FixedUpdate () {
var x:Number = Input.GetAxis("Horizontal");
var y:Number = Input.GetAxis("Vertical");
var down = transform.TransformDirection (Vector3.down);
currentSpeed = rigidbody.velocity.magnitude;
var groundHit : RaycastHit;
if (Physics.Raycast (transform.position, down, groundHit, 2)) {
forward = Vector3.Cross(transform.right ,groundHit.normal );
transform.rotation = Quaternion.LookRotation(forward, groundHit.normal);
//Smooth the ground-following!----------------
//can't get this to work
//targetRotation = Quaternion.LookRotation(forward, groundHit.normal);
//transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 10);
//Arghhh-------------------------------------------
rigidbody.drag = 0.5;
//Throttle
//rigidbody.AddRelativeForce (Vector3.forward * y * forwardSpeed);
rigidbody.AddForce (forward * y * forwardSpeed);
currentVel = rigidbody.velocity.normalized;
slipAngle = Vector3.Angle(transform.forward, currentVel);
//SlipDriving
if(slipAngle > 40){
rigidbody.AddForce(-rigidbody.velocity.normalized * currentSpeed * slipAngle * 0.01);
}
//Grip-Driving
dirNum = AngleDir(transform.forward, currentVel, transform.up);
if(slipAngle > 5){
gripAssist = currentSpeed * (1/slipAngle) * dirNum * 8;
rigidbody.AddRelativeForce(Vector3.left * gripAssist);
}
else{
gripAssist = 0;
}
//Steer
if(currentSpeed > 5){
rigidbody.AddRelativeTorque (Vector3.up * x * turnSpeed * currentSpeed);
}
else{
rigidbody.AddRelativeTorque (Vector3.up * x * turnSpeed * 20);
}
}
else{
// Flight $$anonymous$$ode
//Point-in-direction-of-flight
//transform.forward = currentVel;
var flightDir = Quaternion.LookRotation(currentVel, Vector3.up);
transform.rotation = Quaternion.Slerp(transform.rotation, flightDir, Time.deltaTime * 0.4);
NoSmoke();
rigidbody.drag = 0;
}
//Road Gravity------------------
var hit : RaycastHit;
if (Physics.Raycast (transform.position, down, hit, 30.0)) {
if(hit.distance < 20 && hit.distance > 0.5){
rigidbody.AddRelativeForce (Vector3.down * 20);
}
}
else{
rigidbody.AddForce (Vector3.down * 5);
}
//------------------------------
}
function AngleDir(fwd: Vector3, targetDir: Vector3, up: Vector3) {
var perp: Vector3 = Vector3.Cross(fwd, targetDir);
var dir: float = Vector3.Dot(perp, up);
if (dir > 0.0) {
return 1.0;
} else if (dir < 0.0) {
return -1.0;
} else {
return 0.0;
}
}
Your answer
Follow this Question
Related Questions
smooth follow only on X and Y 1 Answer
SmoothFollow Unity Script improvements? 1 Answer
Unity 2D Object Smooth Follow Problem C# 2 Answers
Smooth Follow Rotation 3 Answers