I’ve been studying the SmoothFollow camera script.
I’d like to change it so that it follows a rigidbody by tracking it’s velocity vector instead of it’s rotation.
Ant thoughts on how to best approach this?
Thanks for any help.
Mitch
I’ve been studying the SmoothFollow camera script.
I’d like to change it so that it follows a rigidbody by tracking it’s velocity vector instead of it’s rotation.
Ant thoughts on how to best approach this?
Thanks for any help.
Mitch
As if the camera were dragged on a spring behind the moving object?
You can do it with a rigidbody on the camera. Give your script a parameter for the “natural” distance between the objects (ie, when the spring is at rest). Find the actual distance between them by subtracting the transform.position of the camera from that of the object and then using the magnitude property of the resulting vector. Then, subtract your “natural” distance from the actual distance to find out how much over or under the actual distance is. The amount over or under is proportional to the force you need to apply to the camera for it to slow down or catch up (multiply it by a “springiness” parameter and tweak this parameter at runtime to tune the effect perfectly). The direction of the force is equal to the position difference vector normalised (ie, the “normalized” property of the vector you calculated to find the distance earlier).
Of course, you may well find there is already a script somewhere that will do all this…
Thanks Andee.
It was a lot easier than I thought it would be:
/*
This camera smoothes out rotation around the y-axis and height.
Horizontal Distance to the target is always fixed.
There are many different ways to smooth the rotation but doing it this way gives you a lot of control over how the camera behaves.
For every of those smoothed values we calculate the wanted value and the current value.
Then we smooth it using the Lerp function.
Then we apply the smoothed values to the transform's position.
*/
// The target we are following
var target : Transform;
// The distance in the x-z plane to the target
var distance = 10.0;
// the height we want the camera to be above the target
var height = 5.0;
// How much we
var heightDamping = 2.0;
var rotationDamping = 3.0;
// Place the script in the Camera-Control group in the component menu
@AddComponentMenu("Camera-Control/Smooth Follow")
partial class SmoothFollow { }
function LateUpdate () {
// Early out if we don't have a target
if (!target)
return;
// Calculate the current rotation angles
wantedRotationAngle = Vector3.Angle(target.rigidbody.velocity, Vector3.forward);
wantedHeight = target.position.y + height;
currentRotationAngle = transform.eulerAngles.y;
currentHeight = transform.position.y;
// Damp the rotation around the y-axis
currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
// Damp the height
currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
// Convert the angle into a rotation
// The quaternion interface uses radians not degrees so we need to convert from degrees to radians
currentRotation = Quaternion.EulerAngles (0, currentRotationAngle * Mathf.Deg2Rad, 0);
// 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;
// Set the height of the camera
transform.position.y = currentHeight;
// Always look at the target
transform.LookAt (target);
}