1 /***
2 *
3 * 核心层: 相机跟随脚本(固定角度)
4 *
5 */
6 using UnityEngine;
7 using System.Collections;
8
9 namespace kernal
10 {
11 public class CameraFollow : MonoBehaviour
12 {
13 // The target we are following
14 public Transform target = null;
15 // The distance in the x-z plane to the target
16 public float distance = 6.0f;
17 // the height we want the camera to be above the target
18 public float height = 8.0f;
19 // How much we
20 public float heightDamping = 4.0f;
21 public float rotationDamping = 0.0f;
22 float a = 2.0f;
23
24
25 void Start()
26 {
27 target = GameObject.FindGameObjectWithTag("Player").transform;
28 }
29
30 void Update()
31 {
32 a -= Time.deltaTime;
33 if (a <= 0)
34 {
35 target = GameObject.FindGameObjectWithTag("Player").transform;
36 a = 2.0f;
37 }
38 }
39
40 void LateUpdate()
41 {
42 // Early out if we don't have a target
43 if (!target) return;
44 // Calculate the current rotation angles
45 var wantedRotationAngle = target.eulerAngles.y;
46 var wantedHeight = target.position.y + height;
47 var currentRotationAngle = transform.eulerAngles.y;
48 var currentHeight = transform.position.y;
49 // Damp the rotation around the y-axis
50 currentRotationAngle = Mathf.LerpAngle(currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
51 // Damp the height
52 currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);
53
54 // Convert the angle into a rotation
55 Quaternion currentRotation = Quaternion.Euler(0, currentRotationAngle, 0);
56 // Set the position of the camera on the x-z plane to:
57 // distance meters behind the target
58 transform.position = target.position;
59 transform.position -= currentRotation * Vector3.forward * distance;
60 // Set the height of the camera
61 //transform.position.y = currentHeight;
62 transform.position = new Vector3(transform.position.x, currentHeight, transform.position.z);
63 // Always look at the target
64 transform.LookAt(target);
65 // Use this for initialization
66 }
67 }
68 }