using UnityEngine;
using System.Collections;
public class _Camera : MonoBehaviour {
private float? distance = 10.0f;//攝像機距離追隨目標(biāo)前后距離
private float height = 5.0f;//攝像機追隨目標(biāo)高度
private float heightDamping = 2.0f;//攝像機上下移動平滑速度
private float rotationDamping = 3.0f;//攝像機旋轉(zhuǎn)平滑速度
public? Transform target;//追隨目標(biāo)
// Use this for initialization
void Start () {
}
void? LateUpdate () {
float wantedRotationAngle = target.eulerAngles.y;//追隨目標(biāo) 歐拉角Y
float wantedHeight = target.position.y + height;//攝像機將要處于的高度
float currentRotationAngle = transform.eulerAngles.y;//攝像機當(dāng)前歐拉角Y
float currentHeight = transform.position.y;//攝像機當(dāng)前高度
// Damp the rotation around the y-axis//緩慢繞Y軸改變到目標(biāo)歐拉角
currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
// Damp the height緩慢移動到目標(biāo)高度
currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
// Convert the angle into a rotation將歐拉角轉(zhuǎn)換為四元數(shù)
Quaternion currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);
// Set the position of the camera on the x-z plane to:使攝像機處于XZ平面
// distance meters behind the target使攝像機處于目標(biāo)后distance處
transform.position = target.position;
transform.position -= currentRotation * Vector3.forward * distance;
// Set the height of the camera設(shè)置攝像機高度
transform.position = new Vector3 (transform.position.x, currentHeight,transform.position.z);
// Always look at the target看向目標(biāo)
transform.LookAt (target);
}
}