Game Development Reference
In-Depth Information
This project can be found in the topic's companion iles (code
bundle) inside the Camera_Smooth_Damp folder of this chapter.
For such cameras, a simple follow behavior is usually not enough for your
purposes. If it were, you could simply parent the camera to the object and leave it
at that. However, typically, you'll want some degree of smoothing or damping to the
camera motion, that is, a falling-off of speed that allows the camera to gradually slow
down to a stop on reaching the target, as opposed to a sudden and immediate stop
in which the camera is either travelling at full speed or not at all. To achieve this,
the Quaternion.Slerp and Vector3.SmoothDamp functions can be used. Consider
the following code sample 5-11 for a class that can be attached to any camera to
smoothly follow an object:
using UnityEngine;
using System.Collections;
//---------------------------------------------------------------
public class CamFollow : MonoBehaviour
{
//---------------------------------------------------------------
//Follow target
public Transform Target = null;
//Reference to local transform
private Transform ThisTransform = null;
//Linear distance to maintain from target (in world units)
public float DistanceFromTarget = 10.0f;
//Height of camera above target
public float CamHeight = 1f;
//Damping for rotation
public float RotationDamp = 4f;
//Damping for position
public float PosDamp = 4f;
//---------------------------------------------------------------
void Awake()
{
//Get transform for camera
ThisTransform = GetComponent<Transform>();
}
 
Search WWH ::




Custom Search