Game Development Reference
In-Depth Information
1.
using UnityEngine;
2.
using System.Collections; 3.
4.
public class PlayerTracking : MonoBehaviour {
5.
//We need a reference to character transform
6.
public Transform playerCharacter;
7.
//Max movement distance to right before camera start following
8.
public float maxDistanceRight = 1.5f;
9.
//Max movement distance to left before camera start following
10.
public float maxDistanceLeft = 1.5f;
11.
//Max movement distance to up before camera start following
12.
public float maxDistanceUp = 1.0f;
13.
//Max movement distance to down before camera start following
14.
public float maxDistanceDown = 1.0f;
15.
16.
// Use this for initialization
17.
void Start () {
18.
19.
}
20.
21.
// Here we use LateUpdate instead of Update
22.
void LateUpdate () {
23.
//Current position of the camera
24.
Vector3 camPos = transform.position;
25.
//Current position of the character
26.
Vector3 playerPos = playerCharacter.position;
27.
28.
//Check if the camera is far behind the character
29.
if(playerPos.x - camPos.x > maxDistanceRight){
30.
camPos.x = playerPos.x - maxDistanceRight;
31.
}
32.
//Check if camera far front of player character
33.
else if(camPos.x - playerPos.x > maxDistanceLeft){
34.
camPos.x = playerPos.x + maxDistanceLeft;
Search WWH ::




Custom Search