/******************************************************** ** project : xx ** date : 05/09/2016 ** auth : Anne ** desc : 镜头更随~ ** Version : 1.0 ********************************************************/ using UnityEngine; using System.Collections; /// /// /// public class CameraSmoothFollow : MonoBehaviour { /// /// 跟随目标 /// public Transform mFollowTarget; /// /// 跟随距离 /// public float mDistance = 0f; /// /// 高度 /// public float mHeight = 0f; /// /// 是否平滑旋转 /// public bool bolSmoothRotation = true; /// /// /// public float damping = 5.0f; /// /// 是否在身后跟随 /// public bool bolFollowBehind = true; /// /// 旋转衰减 /// public float mRotationDamping = 10.0f; /// /// 是否和目标同一个方向 /// public bool bolStaticOffset = false; /// /// 当前方向 /// private Vector3 mWantedPosition = Vector3.zero; /// /// 相机跟随在late,防止Update中逻辑丢失 /// void LateUpdate() { if (bolStaticOffset) { mWantedPosition = mFollowTarget.position + new Vector3(0, mHeight, mDistance); } else { if (bolFollowBehind) { mWantedPosition = mFollowTarget.TransformPoint(0, mHeight, -1 * mDistance); } else { mWantedPosition = mFollowTarget.TransformPoint(0, mHeight, mDistance); } } transform.position = Vector3.Lerp(transform.position, mWantedPosition, Time.deltaTime * damping); if (bolSmoothRotation) { Quaternion mWantedRotation = Quaternion.LookRotation(mFollowTarget.position - transform.position, mFollowTarget.up); transform.rotation = Quaternion.Slerp(transform.rotation, mWantedRotation, Time.deltaTime * mRotationDamping); } else { transform.LookAt(mFollowTarget, mFollowTarget.up); } } }