Game Development Reference
In-Depth Information
// Lock the camera when moving backwards!
// * It is really confusing to do 180 degree spins when turning
around. So We fixed the camera rotation
if (AngleDistance (f_currentAngle, f_targetAngle) > 160 && c_
characterControl.IsMoveBackward ()) {
f_targetAngle += 180;
}
//Apply rotation to the camera
f_currentAngle = Mathf.SmoothDampAngle(f_currentAngle, f_
targetAngle, f_angleVelocity, smoothTime, maxSpeed);
//Update camera height position
f_targetHeight = v3_targetCenter.y + height;
// Damp the height
var f_currentHeight : float = cameraTransform.position.y;
f_currentHeight = Mathf.SmoothDamp (f_currentHeight, f_
targetHeight, f_heightVelocity, heightSmoothTime);
// Convert the angle into a rotation, by which we then
reposition the camera
var q_currentRotation : Quaternion = Quaternion.Euler (0, f_
currentAngle, 0);
// Set the position of the camera on the x-z plane to:
// distance meters behind the target
cameraTransform.position = v3_targetCenter;
cameraTransform.position += q_currentRotation * Vector3.back *
distance;
// Set the height of the camera
cameraTransform.position.y = f_currentHeight;
// Always look at the target
SetUpRotation(v3_targetCenter);
}
5. Finally, we will create the SetupRotation() funcion to update the rotaion of our
camera. Type the following code:
private function SetUpRotation (v3_centerPos : Vector3) {
var v3_cameraPos = cameraTransform.position; //Camera position
var v3_offsetToCenter : Vector3 = v3_centerPos - v3_cameraPos;
//Get the camera center offset
//Generate base rotation only around y-axis
 
Search WWH ::




Custom Search