- Home /
Smooth Follow script and z axis
hello i would like for the camera to smooth follow on the z axis aswell as the other axises whilst looking at my spaceship from a certain distance. The other axis works fines but z is causing me difficulties
var target : Transform;
var distance = 5.0;
var height = 5.0;
var heightDamping = 2.0;
var rotationDamping = 3.0;
@script AddComponentMenu("Camera-Control/Smooth Follow")
function LateUpdate () {
// Early out if we don't have a target
if (!target)
return;
// Calculate the current rotation angles
var wantedRotationAngleY = target.eulerAngles.y;
var currentRotationAngleY = transform.eulerAngles.y;
var wantedRotationAngleX = target.eulerAngles.x;
var currentRotationAngleX = transform.eulerAngles.x;
var wantedRotationAngleZ = target.eulerAngles.z;
var currentRotationAngleZ = transform.eulerAngles.z;
// Damp the rotation around the y-axis
currentRotationAngleY = Mathf.LerpAngle (currentRotationAngleY, wantedRotationAngleY, rotationDamping * Time.deltaTime);
currentRotationAngleX = Mathf.LerpAngle (currentRotationAngleX, wantedRotationAngleX, rotationDamping * Time.deltaTime);
currentRotationAngleZ = Mathf.LerpAngle (currentRotationAngleZ, wantedRotationAngleZ, rotationDamping * Time.deltaTime);
// Damp the height
//currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
// Convert the angle into a rotation
var currentRotation = Quaternion.Euler (currentRotationAngleX, currentRotationAngleY, currentRotationAngleZ);
// Set the position of the camera on the x-z plane to:
// distance meters behind the target
transform.position = target.position;
transform.position -= currentRotation * Vector3.forward * distance;
transform.rotation= currentRotation ;
// Always look at the target
transform.LookAt (target);
}
Answer by theUndeadEmo · Apr 02, 2012 at 10:51 AM
problem solved.
this was causing the problem from the above code
transform.LookAt (target);
i am assuming it always makes you look at the target at an exact z rotation
Answer by Posly · Apr 02, 2012 at 12:45 AM
I tried creating a camera like this before but I also got stumped on the Z-axis, but there's a script called "SmoothFollow" in the Standard Assets folder that does the trick!
this is that script, it doen't let you have a z rotaion
Answer by Harabeck · Apr 05, 2012 at 09:42 PM
Just modifying the original script like that doesn't allow you to properly set height anymore, and the camera freaks out at certain angles because they're using the euler angles. Here's my solution:
//Get relevant rotations
Quaternion currentRotation=transform.rotation;
Quaternion wantedRotation=target.rotation;
//Lerp to new rotation
Quaternion newRotation=Quaternion.Lerp(currentRotation, wantedRotation, rotationDamping*Time.deltaTime);
//set rotation
transform.rotation=newRotation;
//set position: back distance, up height
transform.position=target.position-(transform.forward*distance)+(target.up*height);