unity围绕物体旋转、缩放、移动
注意三个物体的位置!!!
using System;
using UnityEngine;
using UnityEngine.EventSystems;
public class Obj_Rotation : MonoBehaviour
{
[Header("摄像机父物体位置信息")]
public Transform camPrent_tranasform;
[Header("摄像机位置信息")]
public Transform cam_transform;
[Header("旋转参数")]
public Camera_Rotation_Parameter rotation_Parameter;
[Header("缩放参数")]
public Camera_Zoom_Parameter zoom_parameter;
[Header("拖拽参数")]
public Camera_Drag_Parameter drag_parameter;
[Header("鼠标在UI上是否能操作")]
public bool isUI;
//旋转参数
float rotatedAngleX, rotatedAngleY; //预设角度(当前角度加上将要增加/减少的角度)
//缩放参数
private float cameraDistance;//摄像机的初始位置z轴
private float targetCameraDistance;//目标摄像机距离
//拖拽参数
float delta_x, delta_y;
Quaternion Drag_rotation;
//重置参数
private Vector3 cam_pos;
private Vector3 camParent_pos;
private Quaternion camParent_rot;
public float zoom_Min = 5;
public float zoom_Max = 30;
void Start()
{
// camPrent_tranasform = transform.GetChild(0).transform;
// cam_transform = transform.GetComponentInChildren<Camera>().gameObject.transform;
cameraDistance = cam_transform.localPosition.z;
targetCameraDistance = cameraDistance;
rotation_Parameter = new Camera_Rotation_Parameter(true, 0f, 80f, false, 80f, -80f, 5f);
zoom_parameter = new Camera_Zoom_Parameter(5, 30, 1, 1);
drag_parameter = new Camera_Drag_Parameter(true, 0.5f);
zoom_parameter.minDistance = zoom_Min;
zoom_parameter.maxDistance = zoom_Max;
cam_pos = cam_transform.position;
camParent_pos = camPrent_tranasform.localPosition;
camParent_rot = camPrent_tranasform.localRotation;
}
void Update()
{
if (EventSystem.current == null)
{
Debug.LogError("缺少EventSystem");
return;
}
if (!EventSystem.current.IsPointerOverGameObject() || isUI)
{
Oribit();
Zoom();
Drag();
}
}
/// <summary>
/// 拖动
/// </summary>
private void Drag()
{
if (Input.GetMouseButton(0) && drag_parameter.isDrag)//&& !EventSystem.current.IsPointerOverGameObject())
{
delta_x = Input.GetAxis("Mouse X") * drag_parameter.DragSpeed;
delta_y = Input.GetAxis("Mouse Y") * drag_parameter.DragSpeed;
Drag_rotation = Quaternion.Euler(0, transform.rotation.eulerAngles.y, 0);
transform.position = Drag_rotation * new Vector3(-delta_x, -delta_y, 0) + transform.position;
}
}
/// <summary>
/// 旋转
/// </summary>
private void Oribit()
{
if (Input.GetMouseButton(1))
{
var mouse_x = Input.GetAxis("Mouse X");//获取鼠标X轴移动
var mouse_y = -Input.GetAxis("Mouse Y");//获取鼠标Y轴移动
//上下旋转限制
rotatedAngleX = transform.eulerAngles.x + mouse_y * rotation_Parameter.mouseMoveRatio;
if (rotatedAngleX > rotation_Parameter.minXAngle && rotatedAngleX < rotation_Parameter.maxXAngle || !rotation_Parameter.limitXAngle)
{
transform.RotateAround(camPrent_tranasform.position, transform.right, mouse_y * rotation_Parameter.mouseMoveRatio);
}
else
{
transform.RotateAround(camPrent_tranasform.position, transform.right, 0);
}
//左右旋转限制
rotatedAngleY = transform.eulerAngles.y + mouse_x * rotation_Parameter.mouseMoveRatio;
if (rotatedAngleY > rotation_Parameter.minYAngle && rotatedAngleY < rotation_Parameter.maxYAngle || !rotation_Parameter.limitYAngle)
{
transform.RotateAround(camPrent_tranasform.position, Vector3.up, mouse_x * rotation_Parameter.mouseMoveRatio);
}
else
{
transform.RotateAround(camPrent_tranasform.position, Vector3.up, 0);
}
}
}
/// <summary>
/// 镜头拉远或缩进
/// </summary>
private void Zoom()
{
if (Input.GetAxis("Mouse ScrollWheel") != 0)
{
cameraDistance = -cam_transform.localPosition.z;
targetCameraDistance = cameraDistance - Input.GetAxis("Mouse ScrollWheel") * cameraDistance * zoom_parameter.mouseScroollRatio;
targetCameraDistance = Mathf.Clamp(targetCameraDistance, zoom_parameter.minDistance, zoom_parameter.maxDistance);
}
if (Mathf.Abs(targetCameraDistance - cameraDistance) > 0.1f)
{
cameraDistance = Mathf.Lerp(cameraDistance, targetCameraDistance, Time.fixedDeltaTime * zoom_parameter.zoomSensitive);
cam_transform.localPosition = new Vector3(0f, 0f, -cameraDistance);
}
}
/// <summary>
/// 重置位置
/// </summary>
public void Reset_Transform()
{
camPrent_tranasform.position = camParent_pos;
camPrent_tranasform.rotation = camParent_rot;
cam_transform.position = cam_pos;
cameraDistance = cam_pos.z;
targetCameraDistance = cameraDistance;
//拖动
delta_x = 0; delta_y = 0;
Drag_rotation = new Quaternion(0, 0, 0, 0);
rotatedAngleX = 0; rotatedAngleY = 0;
}
}
#region 结构体
/// <summary>
/// 摄像机旋转参数
/// </summary>
[Serializable]
public struct Camera_Rotation_Parameter
{
[Header("旋转速度")]
public float mouseMoveRatio;
[Header("是否限制X轴旋转角度")]
public bool limitXAngle;
[Header("X轴旋转角度最小值")]
public float minXAngle;
[Header("X轴旋转角度最大值")]
public float maxXAngle;
[Header("是否限制Y轴旋转角度")]
public bool limitYAngle;
[Header("Y轴旋转角度最小值")]
public float minYAngle;
[Header("Y轴旋转角度最大值")]
public float maxYAngle;
public Camera_Rotation_Parameter(
bool limitXAngle = true
, float minXAngle = 0f
, float maxXAngle = 60f
, bool limitYAngle = false
, float minYAngle = 0f
, float maxYAngle = 0f
, float mouseMoveRatio = 5f)
{
this.limitXAngle = limitXAngle;
this.minXAngle = minXAngle;
this.maxXAngle = maxXAngle;
this.limitYAngle = limitYAngle;
this.minYAngle = minYAngle;
this.maxYAngle = maxYAngle;
this.mouseMoveRatio = mouseMoveRatio;
}
}
/// <summary>
/// 摄像机视角缩放参数
/// </summary>
[Serializable]
public struct Camera_Zoom_Parameter
{
[Header("缩放最小值")]
public float minDistance;//5
[Header("缩放最大值")]
public float maxDistance;//30
[Header("缩放幅度")]
public float mouseScroollRatio;//1
[Header("缩放平滑度")]
public float zoomSensitive;//1
public Camera_Zoom_Parameter(float minDistance, float maxDistance, float mouseScroollRatio, float zoomSensitive)
{
this.minDistance = minDistance;
this.maxDistance = maxDistance;
this.mouseScroollRatio = mouseScroollRatio;
this.zoomSensitive = zoomSensitive;
}
}
/// <summary>
/// 摄像机视角拖拽参数
/// </summary>
[Serializable]
public struct Camera_Drag_Parameter
{
[Header("是否允许拖拽")]
public bool isDrag;
[Header("拖拽移动速度")]
public float DragSpeed;
public Camera_Drag_Parameter(bool isDrag, float DragSpeed)
{
this.isDrag = isDrag;
this.DragSpeed = DragSpeed;
}
}
#endregion