Unity 各种相机控制脚本:
01
// █████████████████████████████████████████████████████████████████████████████████████████████████████████████████████
// ███ INTERNAL CLASSES ████████████████████████████████████████████████████████████████████████████████████████████████
// █████████████████████████████████████████████████████████████████████████████████████████████████████████████████████
using UnityEngine;
public enum CamViewMode
{
FREE,//自由视角
TOP,//俯视角
LIMIT//固定视角
}
[AddComponentMenu("CameraControl/CameraControllerEvent")]
public class CameraControllerEvent1 : MonoBehaviour
{
//相机物体
private Transform camTrans;
/// <summary>
/// 是否是第一次点击
/// </summary>
private bool _isFirstClick = true;
/// <summary>
/// 相机视角模式
/// </summary>
public CamViewMode viewMode = CamViewMode.FREE;
[SerializeField]
private Vector3 _resetTrans;//相机重置位置
[SerializeField]
private Vector3 _resetAngles;//相机重置角度
[Header("键盘移动速度")]
public float m_speed = 3f;
[Header("鼠标中键移动速度")]
public float m_mSpeed = 5f;
[Header("旋转速度")]
public float m_rSpeed = 5f;
[Header("缩放速度")]
public float m_sSpeed = 5f;
[Header("最大缩放距离")]
public float m_maxDistance = 100f;
[Header("中键移动的缓动值")]
public float moveSmoothing = 2f;
private float m_deltX = 0f;//计算右键旋转
private float m_deltY = 0f;//计算右键旋转
void Start()
{
camTrans = transform;
}
void Update()
{
//在UI上时不执行
//if (EventSystem.current.IsPointerOverGameObject()) return;
if (viewMode != CamViewMode.LIMIT)
{
//有的没加缓动效果
CameraKeyMove();
CameraMiddleMove();
CameraRotate();
CameraScale();
}
//不同视角
CameraMode();
//相机复位
if (Input.GetKeyUp(KeyCode.Space))
{
CameraReset();
}
}
private void CameraMode()
{
switch (viewMode)
{
case CamViewMode.TOP:
camTrans.localRotation = Quaternion.Euler(90, camTrans.localRotation.eulerAngles.y, camTrans.localRotation.eulerAngles.z);
break;
default:
break;
}
}
void CameraScale()
{
//鼠标滚轮场景缩放;
if (Input.GetAxis("Mouse ScrollWheel") != 0)
{
float m_distance = Input.GetAxis("Mouse ScrollWheel") * m_sSpeed;
Vector3 newPos = camTrans.localPosition + camTrans.forward * m_distance;
Debug.Log(newPos.magnitude);
if (newPos.magnitude >= m_maxDistance) return;
camTrans.localPosition = newPos;
}
}
void CameraRotate()
{
//鼠标右键点下控制相机旋转;
if (Input.GetMouseButton(1) || Input.GetMouseButton(0))
{
if (!_isFirstClick)
{
m_deltX += Input.GetAxis("Mouse X") * m_rSpeed;
m_deltY -= Input.GetAxis("Mouse Y") * m_rSpeed;
}
else//第一次点击时规划角度
{
_isFirstClick = false;
m_deltX = _resetAngles.y;
m_deltY = _resetAngles.x;
}
m_deltX = ClampAngle(m_deltX, -360, 360);
m_deltY = ClampAngle(m_deltY, -70, 70);
camTrans.localRotation = Quaternion.Euler(m_deltY, m_deltX, 0);
}
}
void CameraKeyMove()
{
//键盘移动
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
{
camTrans.Translate(Vector3.forward * Time.deltaTime * m_speed);
}
if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
{
camTrans.Translate(Vector3.back * Time.deltaTime * m_speed);
}
if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
{
camTrans.Translate(Vector3.left * Time.deltaTime * m_speed);
}
if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
{
camTrans.Translate(Vector3.right * Time.deltaTime * m_speed);
}
}
void CameraMiddleMove()
{
//点击鼠标中键控制移动;
if (Input.GetMouseButton(2))
{
float dx = Input.GetAxis("Mouse X");
float dy = Input.GetAxis("Mouse Y");
Vector3 yz = camTrans.forward + camTrans.up;
yz.y = 0;
Vector3 TargetLookAt = camTrans.position;
TargetLookAt -= (yz * dy + transform.right * dx) * m_mSpeed;
camTrans.position = Vector3.Lerp(camTrans.position, TargetLookAt, Time.deltaTime * moveSmoothing);
}
}
public void CameraReset()
{
//相机位置和角度重置,需要自己设一个初始的位置和角度
camTrans.localPosition = Vector3.Lerp(camTrans.localPosition, _resetTrans,Time.deltaTime * m_speed);
camTrans.localRotation = Quaternion.Euler(_resetAngles);
}
//规划角度;
float ClampAngle(float angle, float minAngle, float maxAgnle)
{
if (angle <= -360)
angle += 360;
if (angle >= 360)
angle -= 360;
return Mathf.Clamp(angle, minAngle, maxAgnle);
}
}
02
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraControllerEvent2 : MonoBehaviour
{
//距离
public float distance = 15;
//横向角度
public float rot = 0;
//纵向角度 30d度
public float roll = 30f * Mathf.PI * 2 / 360;
//目标物体
public GameObject target;
//横向旋转速度
public float rotSpeed = 0.1f;
//纵向旋转角度
public float maxRoll = 70f * Mathf.PI * 2 / 360;
public float minRoll = 0f * Mathf.PI * 2 / 360;
//纵向旋转速度
private float rollSpeed = 0.1f;
//距离范围
public float maxDistance = 22f;
public float minDistance = 5f;
//距离变化速度
public float zoomSpeed = 0.2f;
private void Start()
{
SetTarget(target);
}
private void LateUpdate()
{
if (target == null)
return;
if (Camera.main == null)
return;
//横向旋转
Rotate();
//纵向旋转
Roll();
//缩放
Zoom();
//目标的坐标
Vector3 targetPos = target.transform.position;
//用三角函数计算相机的位置
Vector3 cameraPos;
float d = distance * Mathf.Cos(roll);
float height = distance * Mathf.Sin(roll);
cameraPos.x = targetPos.x + d * Mathf.Cos(rot);
cameraPos.z = targetPos.z + d * Mathf.Sin(rot);
cameraPos.y = targetPos.y + height;
Camera.main.transform.position = cameraPos;
Camera.main.transform.LookAt(target.transform);
}
//设置目标
public void SetTarget(GameObject target)
{
if (target.transform.Find("cameraPoint") != null)
this.target = target.transform.Find("cameraPoint").gameObject;
else
this.target = target;
}
//横向旋转
public void Rotate()
{
float w = Input.GetAxis("Mouse X") * rotSpeed;
rot -= w;
}
//纵向旋转
public void Roll()
{
float w = Input.GetAxis("Mouse Y") * rollSpeed;
roll -= w;
if (roll > maxRoll)
roll = maxRoll;
if (roll < minRoll)
roll = minRoll;
}
//调整距离
public void Zoom()
{
if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
if (distance > minDistance)
distance -= zoomSpeed;
}
else if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
if (distance < maxDistance)
distance += zoomSpeed;
}
}
}
03
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraControllerEvent3 : MonoBehaviour
{
public GameObject target;
public float distance = 0f;
public float xSpeed = 250.0f;
public float ySpeed = 120.0f;
public float yMinLimit = -20;
public float yMaxLimit = 80;
public float x = 0.0f;
public float y = 0.0f;
public float prevDistance = 0f;
void Start()
{
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
}
void LateUpdate()
{
//if (distance < 2) distance = 2;
distance -= Input.GetAxis("Mouse ScrollWheel") * 2;
if (target && (Input.GetMouseButton(0) || Input.GetMouseButton(1)))
{
var pos = Input.mousePosition;
var dpiScale = 1f;
if (Screen.dpi < 1) dpiScale = 1;
if (Screen.dpi < 200) dpiScale = 1;
else dpiScale = Screen.dpi / 200f;
if (pos.x < 380 * dpiScale && Screen.height - pos.y < 250 * dpiScale) return;
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
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, -distance) + target.transform.position;
transform.rotation = rotation;
transform.position = position;
}
else
{
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
//Screen.lockCursor = false;
}
if (Math.Abs(prevDistance - distance) > 0.001f)
{
prevDistance = distance;
var rot = Quaternion.Euler(y, x, 0);
var po = rot * new Vector3(0.0f, 0.0f, -distance) + target.transform.position;
transform.rotation = rot;
transform.position = po;
}
}
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);
}
}
04
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraControllerEvent4 : MonoBehaviour
{
private Vector3 oldMousePos;
private Vector3 newMosuePos;
[SerializeField]
private float minimumY = 0.2f;
[SerializeField]
private float zoomSpeed = 30.0f;
[SerializeField]
private float keyBoardMoveSpeed = 1f;
[SerializeField]
private float rotSpeed = 0.05f;
[SerializeField]
//private float mouseMoveSpeed = 0.05f;//鼠标中键控制相机的速度
private float distance = 5;
//private Vector3 centerOffset = Vector3.zero;
private Vector3 initPos = Vector3.zero;
private Vector3 initRot = Vector3.zero;
private void Awake()
{
initPos = transform.position;
initRot = transform.eulerAngles;
}
private void OnEnable()
{
transform.position = initPos;
transform.eulerAngles = initRot;
}
void Update()
{
MoveCameraKeyBoard();
ZoomCamera();
SuperViewMouse();
oldMousePos = Input.mousePosition;
}
private void MoveCameraKeyBoard()
{
if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))//(Input.GetAxis("Horizontal")<0)
{
transform.Translate(new Vector3(-keyBoardMoveSpeed, 0, 0), Space.Self);
}
if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
{
transform.Translate(new Vector3(keyBoardMoveSpeed, 0, 0), Space.Self);
}
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
{
transform.Translate(new Vector3(0, 0, keyBoardMoveSpeed), Space.Self);
}
if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
{
transform.Translate(new Vector3(0, 0, -keyBoardMoveSpeed), Space.Self);
}
if (Input.GetKey(KeyCode.E))
{
transform.Translate(new Vector3(0, keyBoardMoveSpeed, 0), Space.World);
}
if (Input.GetKey(KeyCode.Q))
{
if (transform.transform.position.y - keyBoardMoveSpeed >= this.minimumY)
transform.Translate(new Vector3(0, -keyBoardMoveSpeed, 0), Space.World);
}
}
private void ZoomCamera()
{
float offset = Input.GetAxis("Mouse ScrollWheel");
if (offset != 0)
{
offset *= zoomSpeed;
//currentCamera.transform.position = currentCamera.transform.position + currentCamera.transform.forward * offset;//localPosition
this.distance -= offset;
transform.Translate(Vector3.forward * offset, Space.Self); //
}
}
private void SuperViewMouse()
{
if (Input.GetMouseButton(1))
{
newMosuePos = Input.mousePosition;
Vector3 dis = newMosuePos - oldMousePos;
float angleX = dis.x * rotSpeed;//* Time.deltaTime
float angleY = dis.y * rotSpeed;//* Time.deltaTime
transform.Rotate(new Vector3(-angleY, 0, 0), Space.Self);
transform.Rotate(new Vector3(0, angleX, 0), Space.World);
}
}
}
05
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class CameraControllerEvent5 : MonoBehaviour
{
public enum CameraMode
{
Free,
LookAt,
}
public CameraMode cameraMode = CameraMode.Free;
#region ---------------------------- 自由模式相关 ----------------------------
/// <summary>
/// 自由模式:平移的速度 //
/// </summary>
public float translationSpeedOfFree = 1.0f;
/// <summary>
/// 自由模式:旋转的速度 //
/// </summary>
public float rotationSpeedOfFree = 5.0f;
/// <summary>
/// 自由模式:拉近、拉远的速度 //
/// </summary>
public float zoomSpeedOfFree = 6.0f;
/// <summary>
/// 限制旋转X轴的角度范围 //
/// </summary>
public float[] limitEulerAnglesXOfFree = new float[] { -360.0f, 360.0f };
/// <summary>
/// 限制旋转Y轴的角度范围 //
/// </summary>
public float[] limitEulerAnglesYOfFree = new float[] { -89.0f, 89.0f };
private Vector3 originCameraPositionOfFree;
private Vector3 originCameraEulerAnglesOfFree;
private float axisMouseXOfFree;
private float axisMouseYOfFree;
private float translationDeltaXOfFree = 0.0f;
private float translationDeltaYOfFree = 0.0f;
private float translationDeltaZOfFree = 0.0f;
private float rotationDeltaXOfFree = 0.0f;
private float rotationDeltaYOfFree = 0.0f;
#endregion
#region ---------------------------- 观察模式相关 ----------------------------
public Transform transfrom_TargetOfLookAt;
/// <summary>
/// 观察模式:X轴旋转的速度 //
/// </summary>
public float rotationSpeedXOfLookAt = 250.0f;
/// <summary>
/// 观察模式:Y轴旋转的速度 //
/// </summary>
public float rotationSpeedYOfLookAt = 120.0f;
/// <summary>
/// 观察模式:拉近、拉远的速度 //
/// </summary>
public float zoomSpeedOfLookAt = 6.0f;
public float originDistanceOfLookAt = 6.0f;
public float originXOfLookAt = 130.0f;
public float originYOfLookAt = 15.52f;
/// <summary>
/// 限制旋转Y轴的角度范围 //
/// </summary>
public float[] limitEulerAnglesYOfLookAt = new float[] { 1.0f, 90.0f };
/// <summary>
/// 相距目标的距离范围 //
/// </summary>
public float[] limitDistanceOfLookAt = new float[] { 1.0f, 30.0f };
private float axisMouseXOfLookAt;
private float axisMouseYOfLookAt;
private float reuseDistanceOfLookAt;
private Vector3 reuseVector3OfLookAt;
private Quaternion reuseQuaternionOfLookAt;
#endregion
private Camera camera_This;
private Transform transform_ThisCamera;
private Ray reuseRay;
private RaycastHit reuseRaycastHit;
void Awake()
{
camera_This = this.GetComponent<Camera>();
transform_ThisCamera = camera_This.transform;
originCameraPositionOfFree = transform_ThisCamera.position;
originCameraEulerAnglesOfFree = transform_ThisCamera.eulerAngles;
}
void LateUpdate()
{
// 重置 //
if (Input.GetKeyUp(KeyCode.Space))
{
if (cameraMode == CameraMode.Free)
{
ResetFreeMode();
}
else if (cameraMode == CameraMode.LookAt)
{
ResetLookAtMode();
}
}
if (Input.GetKeyUp(KeyCode.F))
{
cameraMode = CameraMode.Free;
ResetFreeMode();
}
if (Input.GetKeyUp(KeyCode.L))
{
cameraMode = CameraMode.LookAt;
ResetLookAtMode();
}
if (cameraMode == CameraMode.Free)
{
OnFreeMode();
}
else if (cameraMode == CameraMode.LookAt)
{
OnLookAtMode();
}
}
#region ---------------------------- 自由模式相关 ----------------------------
/// <summary>
/// 重置自由模式 //
/// </summary>
void ResetFreeMode()
{
transform_ThisCamera.position = originCameraPositionOfFree;
transform_ThisCamera.eulerAngles = originCameraEulerAnglesOfFree;
}
void OnFreeMode()
{
if (Input.GetMouseButton(1))
{
rotationDeltaXOfFree += Input.GetAxis("Mouse X") * rotationSpeedOfFree;
rotationDeltaYOfFree -= Input.GetAxis("Mouse Y") * rotationSpeedOfFree;
rotationDeltaXOfFree = ClampAngle(rotationDeltaXOfFree, limitEulerAnglesXOfFree[0], limitEulerAnglesXOfFree[1]);
rotationDeltaYOfFree = ClampAngle(rotationDeltaYOfFree, limitEulerAnglesYOfFree[0], limitEulerAnglesYOfFree[1]);
transform_ThisCamera.rotation = Quaternion.Euler(rotationDeltaYOfFree, rotationDeltaXOfFree, 0);
}
if (Input.GetMouseButton(2))
{
axisMouseXOfFree = Input.GetAxis("Mouse X");
if (Mathf.Abs(axisMouseXOfFree - 0.1f) > 0.0f)
{
translationDeltaXOfFree = axisMouseXOfFree * translationSpeedOfFree;
transform_ThisCamera.position = transform_ThisCamera.position + transform_ThisCamera.right * translationDeltaXOfFree * -1;
}
axisMouseYOfFree = Input.GetAxis("Mouse Y");
if (Mathf.Abs(axisMouseYOfFree - 0.1f) > 0.0f)
{
translationDeltaYOfFree = axisMouseYOfFree * translationSpeedOfFree;
transform_ThisCamera.position = transform_ThisCamera.position + transform_ThisCamera.up * translationDeltaYOfFree * -1;
}
}
if (Input.GetAxis("Mouse ScrollWheel") != 0)
{
translationDeltaZOfFree = Input.GetAxis("Mouse ScrollWheel") * zoomSpeedOfFree;
transform_ThisCamera.position = transform_ThisCamera.position + transform_ThisCamera.forward * translationDeltaZOfFree;
}
}
#endregion
#region ---------------------------- 观察模式相关 ----------------------------
/// <summary>
/// 重置自由模式 //
/// </summary>
void ResetLookAtMode()
{
if (transfrom_TargetOfLookAt == null)
{
return;
}
transform.rotation = Quaternion.Euler(originYOfLookAt, originXOfLookAt, 0f);
transform.position = Quaternion.Euler(originYOfLookAt, originXOfLookAt, 0f) * new Vector3(0.0f, 0.0f, -originDistanceOfLookAt) + transfrom_TargetOfLookAt.position;
}
void OnLookAtMode()
{
if (Input.GetMouseButtonUp(0))
{
reuseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(reuseRay, out reuseRaycastHit))
{
transfrom_TargetOfLookAt = reuseRaycastHit.transform;
}
}
if (transfrom_TargetOfLookAt == null)
{
return;
}
if (Input.GetMouseButton(1) || Input.GetAxis("Mouse ScrollWheel") != 0)
{
reuseDistanceOfLookAt = Vector3.Distance(transfrom_TargetOfLookAt.position, transform_ThisCamera.position);
axisMouseXOfLookAt += Input.GetAxis("Mouse X") * rotationSpeedXOfLookAt * 0.02f;
axisMouseYOfLookAt -= Input.GetAxis("Mouse Y") * rotationSpeedYOfLookAt * 0.02f;
axisMouseYOfLookAt = ClampAngle(axisMouseYOfLookAt, limitEulerAnglesYOfLookAt[0], limitEulerAnglesYOfLookAt[1]);
reuseDistanceOfLookAt -= Input.GetAxis("Mouse ScrollWheel") * zoomSpeedOfLookAt;
reuseDistanceOfLookAt = Mathf.Clamp(reuseDistanceOfLookAt, limitDistanceOfLookAt[0], limitDistanceOfLookAt[1]);
reuseQuaternionOfLookAt = Quaternion.Euler(axisMouseYOfLookAt, axisMouseXOfLookAt, 0f);
reuseVector3OfLookAt = Quaternion.Euler(axisMouseYOfLookAt, axisMouseXOfLookAt, 0f) * new Vector3(0.0f, 0.0f, -reuseDistanceOfLookAt) + transfrom_TargetOfLookAt.position;
transform.rotation = reuseQuaternionOfLookAt;
transform.position = reuseVector3OfLookAt;
}
}
#endregion
private float ClampAngle(float angle, float minAngle, float maxAgnle)
{
if (angle <= -360)
angle += 360;
if (angle >= 360)
angle -= 360;
return Mathf.Clamp(angle, minAngle, maxAgnle);
}
}
06
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class CameraControllerEvent7 : MonoBehaviour
{
//----------------- 初始参数 -----------------
public Transform Tanks;
private float distance;
private Vector3 targetPos;
//--旋转速度
private float xSpeed = 50.0f;
private float ySpeed = 50.0f;
//旋转限制
private float rotateMaxX = 90;//最大旋转A轴,包含正负
//旋转记录
private float angleX;
private float angleY;
//--缩放速度
private float wheelSpeed = 10;
//--缩放限制,最大,最小
private float MaxWheel = 8;
private float MinWheel = 0.5f;
//--移动限制
private float moveSpeed = 2;
//y轴 最大值、最小值
private float xMaxMove = 1.5f;
private float xMinMove = -1.5f;
private float yMaxMove = 4.8f;
private float yMinMove = 0;
private float zMaxMove = 2.5f;
private float zMinMove = 0;
float lerpRate = 0.2f;
//临时变量
Vector3 t_Vec3;
//----------------- 系统函数 -----------------
void Start()
{
//初始化中心模块
//初始化相机与中心模型的距离
distance = 5;
//初始化中心物体位置
t_Vec3 = Vector3.zero;
t_Vec3.y = 2.3f;
t_Vec3.z = 1.2f;
Tanks.position = t_Vec3;
//初始化中心物体角度
Tanks.rotation = Quaternion.identity;
targetPos = Tanks.position - Tanks.forward * distance;
//初始化相机位置
transform.position = targetPos;
//初始化相机角度
transform.rotation = Tanks.rotation;
}
private void LateUpdate()
{
//如果没有得到中心模块则此脚本无效
if (Tanks == null) return;
//拖动
if (Input.GetMouseButton(2) || Input.GetKey(KeyCode.LeftControl))
{
t_Vec3 = Time.deltaTime * Input.GetAxis("Mouse X") * moveSpeed * distance * Tanks.right;
t_Vec3 += Time.deltaTime * Input.GetAxis("Mouse Y") * moveSpeed * distance * Tanks.up;
t_Vec3 = Tanks.position - t_Vec3;
t_Vec3.x = Mathf.Clamp(t_Vec3.x, xMinMove, xMaxMove);
t_Vec3.y = Mathf.Clamp(t_Vec3.y, yMinMove, yMaxMove);
t_Vec3.z = Mathf.Clamp(t_Vec3.z, zMinMove, zMaxMove);
Tanks.position = t_Vec3;
targetPos = Tanks.position - Tanks.forward * distance;
}
else if (Input.GetMouseButton(1))
{
//旋转
t_Vec3.x = angleX - Time.deltaTime * Input.GetAxis("Mouse Y") * ySpeed * distance;
t_Vec3.y = angleY + Time.deltaTime * Input.GetAxis("Mouse X") * xSpeed * distance;
if (t_Vec3.x > rotateMaxX)
t_Vec3.x = rotateMaxX;
else if (t_Vec3.x < -rotateMaxX)
t_Vec3.x = -rotateMaxX;
if (t_Vec3.y > 360)
t_Vec3.y -= 360;
else if (t_Vec3.y < -360)
t_Vec3.y += 360;
angleY = t_Vec3.y;
angleX = t_Vec3.x;
Tanks.rotation = Quaternion.Euler(angleX, angleY, 0);
//刷新目标位置
targetPos = Tanks.position - Tanks.forward * distance;
}
else if (Input.GetKeyDown(KeyCode.Escape))
{
//Debug.Log("退出展示程序");
Application.Quit();
}
else
{
//缩进
t_Vec3.x = Input.GetAxis("Mouse ScrollWheel");
if (t_Vec3.x != 0)
{
t_Vec3.x *= Time.deltaTime * wheelSpeed * distance;
t_Vec3.x += distance;
if (t_Vec3.x > MaxWheel)
distance = MaxWheel;
else if (t_Vec3.x < MinWheel)
distance = MinWheel;
else
distance = t_Vec3.x;
//刷新目标位置
targetPos = Tanks.position - Tanks.forward * distance;
}
}
transform.rotation = Quaternion.Lerp(transform.rotation, Tanks.rotation, lerpRate);
transform.position = Vector3.Lerp(transform.position, targetPos, lerpRate);
}
}
07
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class CameraControllerEvent8 : MonoBehaviour
{
// 【使用说明】:参考了网上代码
// 1、拖拽到摄像头 作为摄像头的组件
// 2、运行的时候:
// (1)镜头旋转 右键点击着屏幕 然后移动鼠标 即可旋转屏幕
// (2)镜头缩放 滚轮控制缩放镜头
// (3)手型工具 就像scene里面的手型工具一样 滚轮点击着拖拽屏幕
// (4)镜头复原 空格键就会复用镜头
///【1】用于计算的变量
//旋转变量;
private float m_deltX = 0f;
private float m_deltY = 0f;
//摄像机原始位置 和 旋转角度 给复原使用
private Vector3 m_vecOriPosition;
private Quaternion m_vecOriRotation;
//手型工具:上次点击屏幕的位置
private Vector3 m_vecLasMouseClickPosition;
///【2】用于控制幅度的变量
//缩放幅度;
public float m_fScalingSpeed = 10f;
//镜头旋转幅度;
public float m_fRotateSpeed = 5f;
//手型工具幅度;
public float m_fHandToolSpeed = -0.005f;
public Camera Camera => Camera.main;
void Start()
{
m_vecOriRotation = GetComponent<Camera>().transform.rotation;
m_vecOriPosition = GetComponent<Camera>().transform.position;
}
void Update()
{
//(1)旋转镜头 鼠标右键点下控制相机旋转;
if (Input.GetMouseButton(1))
{
m_deltX += Input.GetAxis("Mouse X") * m_fRotateSpeed;
m_deltY -= Input.GetAxis("Mouse Y") * m_fRotateSpeed;
m_deltX = ClampAngle(m_deltX, -360, 360);
m_deltY = ClampAngle(m_deltY, -70, 70);
GetComponent<Camera>().transform.rotation = Quaternion.Euler(m_deltY, m_deltX, 0);
}
//(2)镜头缩放
//鼠标中键点下场景缩放;
if (Input.GetAxis("Mouse ScrollWheel") != 0)
{
//自由缩放方式;
m_fScalingSpeed = Input.GetAxis("Mouse ScrollWheel") * 10f;
GetComponent<Camera>().transform.localPosition = GetComponent<Camera>().transform.position + GetComponent<Camera>().transform.forward * -m_fScalingSpeed;
}
//(3)手型工具
if (Input.GetMouseButtonDown(2))
{
m_vecLasMouseClickPosition = Input.mousePosition;
}
else if (Input.GetMouseButton(2))
{
Vector3 NowHitPosition = Input.mousePosition;
Vector3 offsetVec = NowHitPosition - m_vecLasMouseClickPosition;
offsetVec = GetComponent<Camera>().transform.rotation * offsetVec;
GetComponent<Camera>().transform.localPosition = GetComponent<Camera>().transform.localPosition + offsetVec * (m_fHandToolSpeed);
m_vecLasMouseClickPosition = Input.mousePosition;
}
//(4)相机复位远点;
if (Input.GetKey(KeyCode.Space))
{
m_deltX = 0f;
m_deltY = 0f;
m_deltX = ClampAngle(m_deltX, -360, 360);
m_deltY = ClampAngle(m_deltY, -70, 70);
m_fScalingSpeed = 10.0f;
GetComponent<Camera>().transform.rotation = m_vecOriRotation;
GetComponent<Camera>().transform.localPosition = m_vecOriPosition;
}
}
//规划角度;
float ClampAngle(float angle, float minAngle, float maxAgnle)
{
if (angle <= -360)
angle += 360;
if (angle >= 360)
angle -= 360;
return Mathf.Clamp(angle, minAngle, maxAgnle);
}
}
标签:控制,Vector3,float,transform,private,相机,Input,public,360 From: https://blog.51cto.com/u_15163225/6038423