function LateUpdate () {
// Early out if we don't have a target
if (!target)
return;
// Calculate the current rotation angles
wantedRotationAngle = target.eulerAngles.y;
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
currentRotation = Quaternion.Euler (0, currentRotationAngle, 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);
}
Now Look At these codes, I don’t understand this part
transform.position -= currentRotation * Vector3.forward * distance;
why does the rotation is multiplied by vector3.forward ???
I don’t understand the Math behind there.
any url on tutorials about multiplying rotation and positions will help. thanks.
yeah, its a stretch. Multipying a Quaternion by a vector gives you a position based off the direction. If that vector is normalized (as Vector3.forward is) then it gives you back a normal based off the rotation.
Think of it this way: If you model was at world zero, facing forward (Quaternion.identity) multiplying the rotation (Quaternion.identity * Vector3.forward) would produce… Vector3.forward. Any other rotation, would give you the forward vector of the rotation.
Lets check out an example… put this on a cube, target another cube, move the other cube around while the game is running and you are in the editor.
var target : Transform;
var damp=2.0;
function Update () {
var currentPos=transform.rotation * Vector3.forward * 10;
var targetPos=Vector3.zero;
if(target) targetPos=target.position;
var rotation=transform.rotation;
transform.LookAt(targetPos);
var lookPos=transform.rotation * Vector3.forward * 10;
transform.rotation=Quaternion.Lerp(rotation, transform.rotation, damp * Time.deltaTime);
var actualPos=transform.rotation * Vector3.forward * 10;
Debug.DrawLine (transform.position, transform.position + currentPos, Color.red);
Debug.DrawLine (transform.position, transform.position + actualPos, Color.yellow);
Debug.DrawLine (transform.position, transform.position + lookPos, Color.green);
// currentPos is the initial rotation
// actualPos is the rotation lerped
// lookPos is the rotation based on where it wants to be
}