Graphics Reference
In-Depth Information
the camera to stay parallel to the ground level and keep good visibility, making the game
easier to play:
myTransform.LookAt( target.position + lookAtAdjustVector );
4.6.2 Top-Down Camera
The TopDown_Camera.cs script is a basic target-following system from a top-down
perspective:
public class TopDown_Camera : MonoBehavior
{
public Transform followTarget;
public Vector3 targetOffset;
public float moveSpeed= 2f;
private Transform myTransform;
void Start ()
{
myTransform= transform;
}
public void SetTarget( Transform aTransform )
{
followTarget= aTransform;
}
void LateUpdate ()
{
if(followTarget!=null)
myTransform.position= Vector3.Lerp( myTransform.
position, followTarget.position + targetOffset,
moveSpeed * Time.deltaTime );
}
}
4.6.2.1 Script Breakdown
After the variable declarations, the Start() function takes care of caching a reference to the
object's transform. The next function is extremely important:
public void SetTarget( Transform aTransform )
{
followTarget= aTransform;
}
The SetTarget() function is used by all of the example games to tell the camera which
object to follow during the game. The LateUpdate() function checks to make sure that we
have a target before it tries to position the camera:
Search WWH ::




Custom Search