本篇博文提供一个摄像机控制类,给大家参考和交流!
using UnityEngine;
using System.Collections;
public class MouseOrbit : MonoBehaviour {
///
/// 得到物体的名字
///
//public string Getobejctname;
public Transform initPosObject;
//public Transform targetObject;
///
/// ---------------限制速度-----------------------
///
public float xSpeed = 350.0f;
public float ySpeed = 250.0f;
///
/// ------------ 限制X和Y的角度-------------------
///
public float xMinLimit = -20.0f;
public float xMaxLimit = 80.0f;
public float yMinLimit = -20.0f;
public float yMaxLimit = 80.0f;
///
/// ---------------限制距离-----------------------
///
public float initDis = 2.0f;
public float minDis = 2.0f;
public float maxDis = 10.0f;
///
/// ------------------鼠标滚轮速度------------------
///
public float wheelSpeed = 5.0f;
public float yMoveSpeed = 1.0f;
public float xMoveSpeed = 1.0f;
public float x = 130.0f;
public float y = 30.0f;
private float distance = 0.0f;
public bool IsOnHove = false ;
// Use this for initialization
void Start ()
{
InitOrbitCamera(initPosObject);
}
// public Transform getOb;
bool zoomFlag = false;
// Update is called once per frame
void Update ()
{
//鼠标浮在NGUI滑条上
if (!IsOnHove)
{
//距离相比较
distance = Vector3.Distance(Vector3.zero,transform.localPosition);
x += Input.GetAxis("Mouse X") * xSpeed * 0.02f; // 鼠标横向输入
x = ClampAngle(x, xMinLimit, xMaxLimit);
y += Input.GetAxis("Mouse Y") * ySpeed * -0.02f; //鼠标纵向输入
y = ClampAngle(y, yMinLimit, yMaxLimit);//得到一个适中 的角度
if (Input.GetMouseButton(0))
{
x += Input.GetAxis("Mouse X") * xSpeed * 0.02f; // 鼠标横向输入
x = ClampAngle(x, xMinLimit, xMaxLimit);
y += Input.GetAxis("Mouse Y") * ySpeed * -0.02f; //鼠标纵向输入
y = ClampAngle(y, yMinLimit, yMaxLimit);//得到一个适中 的角度
}
if (Input.GetMouseButtonDown(1))
{
if (!zoomFlag)
{
this.GetComponent().fieldOfView = 12;
}
else {
this.GetComponent().fieldOfView = 50;
}
zoomFlag = !zoomFlag;
}
distance-= Input.GetAxis("Mouse ScrollWheel")*wheelSpeed;//距离是用滚轮控制
}
//得到适中的距离
distance = Mathf.Clamp(distance,minDis,maxDis);
//得到旋转的角度
if (moveflag)
{
Quaternion localRotation = Quaternion.Euler(y, 0, 0);
Vector3 localPosition = localRotation * new Vector3(0.0f, 0.0f, -1*distance) + Vector3.zero;
transform.localRotation = localRotation;
transform.localPosition = localPosition;
Quaternion rotationP = Quaternion.Euler(0,x, 0);
transform.parent.rotation = rotationP;
if(Mathf.Abs(Input.GetAxis("Horizontal"))>0||Mathf.Abs(Input.GetAxis("Vertical"))>0)
{
transform.parent.Translate(new Vector3(Input.GetAxis("Horizontal")*xMoveSpeed, 0, Input.GetAxis("Vertical")*xMoveSpeed)*Time.deltaTime);
}
if(Input.GetMouseButton(2))
{
transform.parent.transform.Translate(0,Input.GetAxis("Mouse Y") * yMoveSpeed,0);
}
}
}
///
/// 设置相机以某个物体为中心
///
///
public void InitOrbitCamera(Transform cObj)
{
if(cObj!=null)
{
transform.parent.position = cObj.position;
}
else
{
transform.parent.position = Vector3.zero;
}
transform.localRotation = Quaternion.Euler(y,x,0);
transform.localPosition = Quaternion.Euler(y,x,0) * new Vector3(0.0f,0.0f,-1*initDis);
}
//限制角度
float ClampAngle(float angle,float min,float max)
{
if (angle < -360.0f)
{
angle += 360.0f;
}
if (angle > 360.0f)
{
angle -= 360.0f;
}
return Mathf.Clamp (angle, min, max);
}
private Vector3 targetPos;
private bool moveflag = true;
private bool backFlag = false;
///
/// 返回目标点
///
///
///
public IEnumerator BackStartPos(Transform obj)
{
backFlag = true;
yield return 1;
StartCoroutine(SetCameraTarget(obj));
yield return 1;
}
public float TempY;
public float TempX;
///
/// 设置相机的目标点
///
///
///
public IEnumerator SetCameraTarget(Transform obj)
{
TempY = y;
TempX = x;
bool isMove = true;
moveflag = false;
Quaternion localRotation = Quaternion.Euler(y, 0, 0);
Quaternion rotationP = Quaternion.Euler(0, x, 0);
Vector3 camEndPos = localRotation * new Vector3(0.0f, 0.0f, -1 * initDis);
Vector3 camPos;
if (obj.transform.GetComponent())
{
camPos = obj.transform.GetComponent().bounds.center;
}
else
{
camPos = obj.transform.position;
}
while (isMove)
{
transform.localRotation = Quaternion.Lerp(transform.localRotation, localRotation,0.05f);
transform.localPosition = Vector3.Lerp(transform.localPosition, camEndPos, 0.05f);
transform.parent.position = Vector3.Lerp(transform.parent.position, camPos, 0.05f);
transform.parent.rotation = Quaternion.Lerp(transform.parent.rotation, rotationP, 0.05f);
yield return new WaitForSeconds(0.01f);
if ((Vector3.Distance(transform.parent.position, camPos) < 0.005f) && (Mathf.Abs((Vector3.Distance(transform.localPosition, Vector3.zero)) - initDis) < 0.01f))
{
isMove = false;
//恢复输入的y的值
y = TempY;
//恢复输入的x的值
x = TempX;
moveflag = true;
transform.parent.position = camPos;
backFlag = false;
IsOnHove = false;
break;
}
}
}
}