下面脚本挂在空物体上,要展示的摄像机放在空物体下。调节参数就可以用了
using UnityEngine;
using System.Collections;
public static class Tool
{
public static Vector3 GetCenter(this Transform root)
{
MeshRenderer[] renderers = root.GetComponentsInChildren<MeshRenderer>();
if (renderers.Length == 0) return root.position;
Bounds bounds = renderers[0].bounds;
for (int i = 1; i < renderers.Length; i++)
{
bounds.Encapsulate(renderers[i].bounds);
}
return bounds.center;
}
}
public class MoveCamera : MonoBehaviour
{
public static MoveCamera instance;
public bool autoRotate = false; //自动旋转
public float autoRotateSpeed = 20;
[SerializeField]
private Transform target; //目标物体
public enum MoveCameraMode
{
Transform,
Position
}
private MoveCameraMode mode = MoveCameraMode.Transform;
private Vector3 targetPosition/*=Tool.GetCenter(target)*/;
public float xSpeed = 200, ySpeed = 200, mSpeed = 10;
public float yMinLimit = 0, yMaxLimit = 80;
public float distance =20, minDistance = 20, maxDistance = 90;
private float startDistance;
public bool startNeedDamp;
public float moveSpeed = 20;
public bool needDamping = true;
float damping = 5.0f;
public float x = 75.14f;
public float y = 36.85999f;
public float offsetX = 0.0f;
public float offsetY = 0.0f;
public float Ymin = 0;
public GameObject m_Camera; //摄像机
[SerializeField]
private float MidSpeed = 5; //中键移动速度
// Use this for initialization
void Start()
{
//startDistance = distance;
//startNeedDamp = needDamping;
//SetTarget(target.transform.GetCenter(), target);
m_Camera = transform.GetChild(0).gameObject;
}
public void InitPosRot()
{
x = 75.14f;
y = 36.85999f;
}
private void OnEnable()
{
InitPosRot();
instance = this;
startDistance = distance;
startNeedDamp = needDamping;
//SetTarget(target.transform.GetCenter(), target);
}
public void SetTarget(Transform t)
{
target = t;
mode = MoveCameraMode.Transform;
}
void SetPornt(GameObject t)
{
MeshRenderer[] renderers = t.GetComponentsInChildren<MeshRenderer>(); //选中物体所有包含Mesh的子物体
if (renderers.Length > 0) //当物体个数至少一个的时候,这个方法才有效
{
Bounds bounds = renderers[0].bounds; //以第一个Mesh的Bounds为计算的开始
for (int i = 1; i < renderers.Length; i++)
{
bounds.Encapsulate(renderers[i].bounds); //依次吞噬其他的Bounds
}
t.transform.position = bounds.center;
}
}
public void SetTarget(Vector3 position, Transform t)
{
targetPosition = position;
target = t;
mode = MoveCameraMode.Position;
}
public void ResetCamera()
{
x = 0;
y = 0;
targetPosition = Vector3.zero;
target = null;
mode = MoveCameraMode.Transform;
distance = startDistance;
autoRotate = false;
needDamping = startNeedDamp;
}
public void DestroyTarget()
{
if (target != null)
{
DestroyImmediate(target.gameObject);
target = null;
}
mode = MoveCameraMode.Transform;
}
int rememberDir = 0;
int rememberFun = 0;
void Update()
{
if (Input.GetKeyUp(KeyCode.F)) //按“F”键 还原物体
{
offsetX = 0;
offsetY = 0;
}
DisposeMouth();
moveSpeed = distance * 1.3f;
if (!autoRotate)
{
if (Input.GetMouseButton(0))
{
x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
}
if (!Input.GetMouseButton(2))
{
float axis = Input.GetAxis("Mouse ScrollWheel") * mSpeed;
if (axis > 0)
{
if (distance > 4)
distance -= axis;
}
else
distance -= axis;
}
}
y = ClampAngle(y, yMinLimit, yMaxLimit);
if (Input.GetKeyDown(KeyCode.LeftBracket))
{
moveSpeed -= 1;
}
if (Input.GetKeyDown(KeyCode.RightBracket))
{
moveSpeed += 1;
}
}
void FixedUpdate()
{
if (autoRotate)
{
x += autoRotateSpeed * 0.02f;
}
}
void LateUpdate()
{
if (mode == MoveCameraMode.Transform && target == null) return;
distance = Mathf.Clamp(distance, minDistance, maxDistance);
Quaternion rotation = Quaternion.Euler(y, x, 0.0f);
//Quaternion rotation = Quaternion.Euler(30, x, 0.0f);
Vector3 disVector = new Vector3(0.0f, 0.0f, -distance);
Vector3 position = Vector3.zero;
if (mode == MoveCameraMode.Transform)
position = rotation * disVector + target.position;
else
position = rotation * disVector + targetPosition;
m_Camera.transform.localPosition = new Vector3(offsetX, offsetY);
if (needDamping)
{
transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * damping);
transform.position = Vector3.Lerp(transform.position, position, Time.deltaTime * damping);
}
else
{
transform.rotation = rotation;
transform.position = position;
}
}
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);
}
/// <summary>
/// 处理鼠标逻辑
/// </summary>
void DisposeMouth()
{
if (Input.GetMouseButton(2))
{
float x = Input.GetAxis("Mouse X");
float y = Input.GetAxis("Mouse Y");
offsetX -= x * MidSpeed * Time.deltaTime;
offsetY -= y * MidSpeed * Time.deltaTime;
//target.transform.localPosition = new Vector3(target.transform.localPosition.x - x * MidSpeed, target.transform.localPosition.y - y * MidSpeed);
}
}
}