這個是須要用的DOTween的,將此腳本掛到相機上,而後再添加相機跟隨的目標便可,具體代碼以下this
using DG.Tweening;
using UnityEngine;
public class FollowTrackingCamera : MonoBehaviour
{
public static FollowTrackingCamera _Instance;
// Camera target to look at.
//相機跟隨目標
public Transform target;
//相機旋轉角度
public Vector3 CameraRotation;
// Exposed vars for the camera position from the target.
//從目標到攝像機位置的外露vars.
public float height = 20f;
public float distance = 20f;
// Camera limits.
//相機移動範圍
public float min = 10f;
public float max = 60;
// Options.
//public bool doRotate;
//相機旋轉以及縮放功能開關
public bool doZoom;
public bool doRotate;
// The movement amount when zooming.縮放時的移動量。
public float zoomStep = 30f;
public float zoomSpeed = 5f;
private float heightWanted;
private float distanceWanted;
public float xSpeed = 3.0f;
public float ySpeed = 3.0f;
public float yMinLimit = -20f;
public float yMaxLimit = 80f;
//public float xMinLimit = 30f;
//public float xMaxLimit = 220f;
public float distanceMin = 1.5f;
public float distanceMax = 15f;
public float smoothTime = 2f;
float rotationYAxis = 230.0f;
float rotationXAxis = -8.0f;
float velocityX = 0.0f;
float velocityY = 0.0f;
//兩根手指
private Touch oldTouch1;
private Touch oldTouch2;
//Vector2 m_screenPos = Vector2.zero; //記錄手指觸碰的位置
float scaleFactor;
// Result vectors.
private Vector3 zoomResult;//縮放後坐標
private Quaternion rotationResult;
private Vector3 targetAdjustedPosition;
private Quaternion rotation;
private void Awake()
{
_Instance = this;
}
void Start()
{
init();
}
void init()
{
Position = transform.position;
rotation = transform.rotation;
//獲得相機歐拉角
Vector3 angles = transform.eulerAngles;
//相機繞Y軸轉動的角度值
rotationYAxis = angles.y;
//相機繞X軸轉動的角度值
rotationXAxis = angles.x;
print("相機初始位置" + rotationXAxis);
//print("Y軸數值"+ rotationYAxis);
//print("X軸數值" + rotationXAxis);
// Initialise default zoom vals.
//相機當前高度賦值於目標高度
heightWanted = height;
distanceWanted = distance;
// Setup our default camera. We set the zoom result to be our default position.
zoomResult = new Vector3(0f, height, -distance);
}
public static float InitAngle=-90;
public float CurrAngle=45;
public float WantedScale = 20;//想要的縮進大小
void LateUpdate()
{
if (IsInit == true)
{
distanceWanted = WantedScale;
AsianAni.Instance.Tween(InitAngle);
rotationXAxis = 25;
//DOTween.To(() => distanceWanted, x => distanceWanted = x, 19, 0.01f);
DOTween.To(() => rotationXAxis, x => rotationXAxis = x, CurrAngle, 0.5f);
rotationYAxis = 0;
IsInit = false;
}
// Check target.
//檢測目標是否存在
if (!target)
{
Debug.LogError("This camera has no target, you need to assign a target in the inspector.");
return;
}
//相機視角縮放
if (doZoom)
{
//print(doRotate);
//if (Input.touchCount <= 0)
//{
// return;
//}
float mouseInput;
if (Input.touchCount > 1)
{
Touch newTouch1 = Input.GetTouch(0);
Touch newTouch2 = Input.GetTouch(1);
//第2點剛開始接觸屏幕, 只記錄,不作處理
if (newTouch2.phase == TouchPhase.Began)
{
oldTouch2 = newTouch2;
oldTouch1 = newTouch1;
//return;
}
//計算老的兩點距離和新的兩點間距離,變大要放大模型,變小要縮放模型
float oldDistance = Vector2.Distance(oldTouch1.position, oldTouch2.position);
float newDistance = Vector2.Distance(newTouch1.position, newTouch2.position);
//兩個距離只差,爲正表示放大,爲負表示縮小
float offset = newDistance - oldDistance;
//縮放因子
scaleFactor = offset / 1000f;
mouseInput = scaleFactor;
heightWanted -= zoomStep * mouseInput;
distanceWanted -= zoomStep * mouseInput;
}
// Record our mouse input. If we zoom add this to our height and distance.
//記錄鼠標滾輪滾動時的變量 並賦值記錄
//mouseInput特性:正常狀態爲0;滾輪前推一格變爲+0.1一次,後拉則變爲-0.1一次
// Input.GetAxis("Mouse ScrollWheel");
if (Input.touchCount <= 0)
{
mouseInput = Input.GetAxis("Mouse ScrollWheel");
heightWanted -= zoomStep * mouseInput;
distanceWanted -= zoomStep * mouseInput;
}
//print("+++"+mouseInput);
// Make sure they meet our min/max values.
//限制相機高度範圍
heightWanted = Mathf.Clamp(heightWanted, min, max);
distanceWanted = Mathf.Clamp(distanceWanted, min, max);
//差值計算,動態修改相機高度值(平滑的變化)
height = Mathf.Lerp(height, heightWanted, Time.deltaTime * zoomSpeed);
distance = Mathf.Lerp(distance, distanceWanted, Time.deltaTime * zoomSpeed);
// Post our result.
//縮放後坐標
zoomResult = new Vector3(0f, height, -distance);
}
//相機視角旋轉
if (doRotate)
{
//print("水平" + Input.GetAxis("Horizontal"));
//print("豎直" + Input.GetAxis("Vertical"));
if (Input.touchCount == 1)
{
Touch newTouch1 = Input.GetTouch(0);
//Touch touch = Input.GetTouch(0);
if (Input.touches[0].phase == TouchPhase.Began)
{
oldTouch1 = newTouch1;
//m_screenPos = touch.position;
}
if (Input.touches[0].phase == TouchPhase.Moved)
{
float CX = newTouch1.position.x - oldTouch1.position.x;
float CY = newTouch1.position.y - oldTouch1.position.y;
velocityX += xSpeed * CX * 0.02f * Time.deltaTime;
velocityY += ySpeed * CY * 0.02f * Time.deltaTime;
}
}
if (Input.GetMouseButton(2) || Input.GetMouseButton(0) || Input.GetMouseButton(1))
{
// print("歐拉角"+transform.eulerAngles);
velocityX += xSpeed * Input.GetAxis("Mouse X") * 0.02f;
velocityY += ySpeed * Input.GetAxis("Mouse Y") * 0.02f;
}
rotationYAxis += velocityX;
rotationXAxis -= velocityY;
if (rotationXAxis >= 90)
{
rotationXAxis = 90;
}
else if (rotationXAxis <= -90)
{
rotationXAxis = -90;
}
}
Quaternion toRotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0);
Quaternion rotation = toRotation;
Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
//相機跟隨
Vector3 position = rotation * negDistance + target.position;
//改變相機Rotation,從而旋轉相機
transform.rotation = rotation;
//將縮放後的座標做爲相機的當前座標位置
transform.position = position;
velocityX = Mathf.Lerp(velocityX, 0, Time.deltaTime * smoothTime);
velocityY = Mathf.Lerp(velocityY, 0, Time.deltaTime * smoothTime);
}
public static float ClampAngle(float angle, float min, float max)
{
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
//限制相機轉動角度
return Mathf.Clamp(angle, min, max);
}
public void InitPoint(http://www.my516.com)
{
heightWanted = max;
distanceWanted = max;
}
public void InitReturn(float a, float b)
{
heightWanted = a;
distanceWanted = b;
}
public Vector3 Position;//當前攝像機的位置
public Vector3 Rotation;//當前攝像機的角度
public bool IsInit = false;
}
--------------------- orm