162 lines
4.4 KiB
C#
162 lines
4.4 KiB
C#
using UnityEngine;
|
||
|
||
public class FreeCameraController : MonoBehaviour
|
||
{
|
||
public static FreeCameraController instance;
|
||
// 相机移动速度
|
||
public float moveSpeed = 5.0f;
|
||
// 相机旋转速度
|
||
public float rotateSpeed = 0.05f;
|
||
// X轴旋转的最大范围
|
||
public float xRotationLimit = 80.0f;
|
||
// 是否启用碰撞检测
|
||
public bool enableCollision = false;
|
||
|
||
private Vector3 lastMousePosition;
|
||
private bool isDragging = false;
|
||
private float xRotation = 0.0f;
|
||
private float yRotation = 0.0f;
|
||
public bool isMov = true;
|
||
public bool isRot = true;
|
||
CharacterController ctrlor;
|
||
|
||
private void Awake()
|
||
{
|
||
instance = this;
|
||
DontDestroyOnLoad(this);
|
||
ctrlor = GetComponent<CharacterController>();
|
||
Global.appSetting.MouseMoveSpeed.RegisterWithInitValue(v => rotateSpeed = v);
|
||
}
|
||
|
||
void Update()
|
||
{
|
||
if (isMov)
|
||
{
|
||
#if VR
|
||
// 相机移动
|
||
float horizontal = Input.GetAxis("Horizontal") * moveSpeed * Time.deltaTime;
|
||
float vertical = Input.GetAxis("Vertical") * moveSpeed * Time.deltaTime;
|
||
|
||
Vector3 move = transform.right * horizontal + transform.forward * vertical;
|
||
|
||
transform.position += move;
|
||
#else
|
||
|
||
// 获取水平面内的前后左右移动方向
|
||
Vector3 moveDirection = Vector3.zero;
|
||
|
||
//W键前进(水平面内)
|
||
if (Input.GetKey(KeyCode.W))
|
||
{
|
||
Vector3 forward = Vector3.ProjectOnPlane(transform.forward, Vector3.up).normalized;
|
||
moveDirection += forward;
|
||
}
|
||
|
||
//S键后退(水平面内)
|
||
if (Input.GetKey(KeyCode.S))
|
||
{
|
||
Vector3 back = -Vector3.ProjectOnPlane(transform.forward, Vector3.up).normalized;
|
||
moveDirection += back;
|
||
}
|
||
|
||
//A键左移(水平面内)
|
||
if (Input.GetKey(KeyCode.A))
|
||
{
|
||
Vector3 left = -Vector3.ProjectOnPlane(transform.right, Vector3.up).normalized;
|
||
moveDirection += left;
|
||
}
|
||
|
||
//D键右移(水平面内)
|
||
if (Input.GetKey(KeyCode.D))
|
||
{
|
||
Vector3 right = Vector3.ProjectOnPlane(transform.right, Vector3.up).normalized;
|
||
moveDirection += right;
|
||
}
|
||
|
||
// 应用移动
|
||
if (moveDirection != Vector3.zero)
|
||
{
|
||
ctrlor.Move(moveDirection * moveSpeed * Time.deltaTime);
|
||
}
|
||
#endif
|
||
}
|
||
|
||
if (isRot)
|
||
{
|
||
if (Input.GetMouseButtonDown(1))
|
||
{
|
||
lastMousePosition = Input.mousePosition;
|
||
isDragging = true;
|
||
SyncRotation();
|
||
}
|
||
|
||
if (Input.GetMouseButtonUp(1))
|
||
{
|
||
isDragging = false;
|
||
}
|
||
|
||
if (isDragging)
|
||
{
|
||
// 相机旋转
|
||
Vector3 mouseDelta = Input.mousePosition - lastMousePosition;
|
||
lastMousePosition = Input.mousePosition;
|
||
|
||
xRotation -= mouseDelta.y * rotateSpeed;
|
||
yRotation += mouseDelta.x * rotateSpeed;
|
||
|
||
// 限制 X 轴旋转范围
|
||
xRotation = Mathf.Clamp(xRotation, -xRotationLimit, xRotationLimit);
|
||
|
||
transform.rotation = Quaternion.Euler(xRotation, yRotation, 0);
|
||
}
|
||
}
|
||
}
|
||
|
||
// 公共方法:旋转相机到指定方向
|
||
public void LookAtPos(Vector3 direction)
|
||
{
|
||
Quaternion targetRotation = Quaternion.LookRotation(direction);
|
||
transform.rotation = targetRotation;
|
||
SyncRotation();
|
||
}
|
||
|
||
public void Rotate(Vector3 eulerAngles)
|
||
{
|
||
transform.eulerAngles = eulerAngles;
|
||
SyncRotation();
|
||
}
|
||
|
||
public void SyncRotation()
|
||
{
|
||
Vector3 currentRotation = transform.eulerAngles;
|
||
xRotation = currentRotation.x;
|
||
yRotation = currentRotation.y;
|
||
|
||
// 归一化 xRotation 和 yRotation
|
||
if (xRotation > 180f)
|
||
{
|
||
xRotation -= 360f;
|
||
}
|
||
else if (xRotation < -180f)
|
||
{
|
||
xRotation += 360f;
|
||
}
|
||
|
||
if (yRotation > 180f)
|
||
{
|
||
yRotation -= 360f;
|
||
}
|
||
else if (yRotation < -180f)
|
||
{
|
||
yRotation += 360f;
|
||
}
|
||
|
||
transform.eulerAngles = new Vector3(xRotation, yRotation, 0);
|
||
}
|
||
|
||
public void SetLock(bool isMov, bool isRot)
|
||
{
|
||
this.isMov = isMov;
|
||
this.isRot = isRot;
|
||
}
|
||
} |