直接掛載到模型上
using UnityEngine;
using System.Collections;
public class OnDrag : MonoBehaviour
{
//目標(biāo)物體
public Transform target;
private int MouseWheelSensitivity = 1; //放大倍數(shù)的快慢
private int MouseZoomMin = 1; //最小倍數(shù)
private int MouseZoomMax = 5; //最大倍數(shù)
//默認(rèn)距離
private float normalDistance = 3;
private Vector3 normalized;
//拖拽的移動速度
private float xSpeed = 250.0f;
private float ySpeed = 120.0f;
//拖拽的高度限制
private int yMinLimit = -20;
private int yMaxLimit = 80;
//角度
private float x = 0.0f;
private float y = 0.0f;
//記錄目標(biāo)物體的坐標(biāo)
private Vector3 screenPoint;
private Vector3 offset;
//圍繞x旋轉(zhuǎn)30°
private Quaternion rotation = Quaternion.Euler(new Vector3(30f, 0f, 0f));
//目標(biāo)的3D坐標(biāo)
private Vector3 CameraTarget;
//打印歐拉角:繞各個軸旋轉(zhuǎn)的角度铃慷,順時針為正方向
public void Awake()
{
target = transform;
print(transform.eulerAngles.x);
print(transform.eulerAngles.y);
print(transform.eulerAngles.z);
}
void Start()
{
//找到目標(biāo)飛機的3d坐標(biāo)
CameraTarget = target.position;
//目標(biāo)飛機的z-3雏搂,距離攝像機更近了
float z = target.transform.position.z - normalDistance;
//給當(dāng)前相機給定位,現(xiàn)在的3D坐標(biāo)乘以30°
transform.position = rotation * new Vector3(transform.position.x, transform.position.y, z);
//將視角轉(zhuǎn)向物體
transform.LookAt(target);
//記錄各個軸偏離的角度
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
}
void OnGUI()
{
GUI.Label(new Rect(10, 10, 300, 30), "左擊:旋轉(zhuǎn)宋渔;滾輪:縮放弹灭;中擊:拖拽");
}
void Update()
{
//如果左擊了督暂,旋轉(zhuǎn)
if (Input.GetMouseButton(0))
{
x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation = Quaternion.Euler(y, x, 0);
var position = rotation * new Vector3(0.0f, 0.0f, -normalDistance) + CameraTarget;
transform.rotation = rotation;
transform.position = position;
}
//滾輪縮放
else if (Input.GetAxis("Mouse ScrollWheel") != 0)
{
//攝像機3d坐標(biāo)-物體的3d坐標(biāo)
normalized = (transform.position - CameraTarget).normalized;
if (normalDistance >= MouseZoomMin && normalDistance <= MouseZoomMax)
{
normalDistance -= Input.GetAxis("Mouse ScrollWheel") * MouseWheelSensitivity;
}
if (normalDistance < MouseZoomMin)
{
normalDistance = MouseZoomMin;
}
if (normalDistance > MouseZoomMax)
{
normalDistance = MouseZoomMax;
}
//改變攝像機的遠(yuǎn)近
transform.position = normalized * normalDistance;
}
//案件按下 記錄鼠標(biāo)的
else if (Input.GetMouseButtonDown(2))
{
//將目標(biāo)物體的坐標(biāo)轉(zhuǎn)化成平面坐標(biāo)
screenPoint = Camera.main.WorldToScreenPoint(target.transform.position);
//計算鼠標(biāo)的3維坐標(biāo)跟物體的3維坐標(biāo)的差值
offset = target.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}
//中鍵拖拽,改變飛機的坐標(biāo)穷吮,每幀調(diào)用
if (Input.GetMouseButton(2))
{
//鼠標(biāo)的平面坐標(biāo)
Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
//鼠標(biāo)轉(zhuǎn)移的3d空間坐標(biāo)值
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
//改變鼠標(biāo)的3D空間坐標(biāo)
target.transform.position = curPosition;
}
//朝向逻翁,每動一幀都要改變朝向
transform.LookAt(CameraTarget);
}
//控制旋轉(zhuǎn)的角度,如果旋轉(zhuǎn)的角度大于360或者小于360都要加上或者減去對應(yīng)的角度
static float ClampAngle(float angle, float min, float max)
{
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp(angle, min, max);
}
}