2024-12-17 14:26:40 +08:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
|
|
public class FreeCameraController : MonoBehaviour
|
|
|
|
|
|
{
|
|
|
|
|
|
public static FreeCameraController instance;
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><C6B6>ٶ<EFBFBD>
|
2025-07-02 14:15:36 +08:00
|
|
|
|
public float moveSpeed = 5.0f;
|
2024-12-17 14:26:40 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD>ٶ<EFBFBD>
|
2025-07-02 14:15:36 +08:00
|
|
|
|
public float rotateSpeed = 0.05f;
|
2024-12-17 14:26:40 +08:00
|
|
|
|
// X<><58><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Χ
|
|
|
|
|
|
public float xRotationLimit = 80.0f;
|
|
|
|
|
|
// <20>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ײ<EFBFBD><D7B2><EFBFBD><EFBFBD>
|
2025-07-02 14:15:36 +08:00
|
|
|
|
public bool enableCollision = false;
|
2024-12-17 14:26:40 +08:00
|
|
|
|
|
|
|
|
|
|
private Vector3 lastMousePosition;
|
|
|
|
|
|
private bool isDragging = false;
|
|
|
|
|
|
private float xRotation = 0.0f;
|
|
|
|
|
|
private float yRotation = 0.0f;
|
2024-12-19 16:38:42 +08:00
|
|
|
|
public bool isMov = true;
|
|
|
|
|
|
public bool isRot = true;
|
2025-03-04 11:54:15 +08:00
|
|
|
|
CharacterController ctrlor;
|
2025-07-02 14:15:36 +08:00
|
|
|
|
|
2024-12-17 14:26:40 +08:00
|
|
|
|
private void Awake()
|
|
|
|
|
|
{
|
|
|
|
|
|
instance = this;
|
|
|
|
|
|
DontDestroyOnLoad(this);
|
2025-03-04 11:54:15 +08:00
|
|
|
|
ctrlor = GetComponent<CharacterController>();
|
2024-12-17 14:26:40 +08:00
|
|
|
|
Global.appSetting.MouseMoveSpeed.RegisterWithInitValue(v => rotateSpeed = v);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Update()
|
|
|
|
|
|
{
|
2025-01-13 11:55:37 +08:00
|
|
|
|
if (isMov)
|
2024-12-17 14:26:40 +08:00
|
|
|
|
{
|
2025-03-19 16:31:17 +08:00
|
|
|
|
#if VR
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD>
|
|
|
|
|
|
float horizontal = Input.GetAxis("Horizontal") * moveSpeed * Time.deltaTime;
|
|
|
|
|
|
float vertical = Input.GetAxis("Vertical") * moveSpeed * Time.deltaTime;
|
2024-12-17 14:26:40 +08:00
|
|
|
|
|
2025-03-19 16:31:17 +08:00
|
|
|
|
Vector3 move = transform.right * horizontal + transform.forward * vertical;
|
2025-03-04 11:54:15 +08:00
|
|
|
|
|
2025-03-19 16:31:17 +08:00
|
|
|
|
transform.position += move;
|
|
|
|
|
|
#else
|
2025-03-04 11:54:15 +08:00
|
|
|
|
|
2025-07-02 14:15:36 +08:00
|
|
|
|
// <20><>ȡˮƽ<CBAE><C6BD><EFBFBD>ڵ<EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
Vector3 moveDirection = Vector3.zero;
|
|
|
|
|
|
|
|
|
|
|
|
//W<><57>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>ˮƽ<CBAE><C6BD><EFBFBD>ڣ<EFBFBD>
|
2025-03-04 11:54:15 +08:00
|
|
|
|
if (Input.GetKey(KeyCode.W))
|
|
|
|
|
|
{
|
2025-07-02 14:15:36 +08:00
|
|
|
|
Vector3 forward = Vector3.ProjectOnPlane(transform.forward, Vector3.up).normalized;
|
|
|
|
|
|
moveDirection += forward;
|
2025-03-04 11:54:15 +08:00
|
|
|
|
}
|
2025-07-02 14:15:36 +08:00
|
|
|
|
|
|
|
|
|
|
//S<><53><EFBFBD><EFBFBD><EFBFBD>ˣ<EFBFBD>ˮƽ<CBAE><C6BD><EFBFBD>ڣ<EFBFBD>
|
2025-03-04 11:54:15 +08:00
|
|
|
|
if (Input.GetKey(KeyCode.S))
|
|
|
|
|
|
{
|
2025-07-02 14:15:36 +08:00
|
|
|
|
Vector3 back = -Vector3.ProjectOnPlane(transform.forward, Vector3.up).normalized;
|
|
|
|
|
|
moveDirection += back;
|
2025-03-04 11:54:15 +08:00
|
|
|
|
}
|
2025-07-02 14:15:36 +08:00
|
|
|
|
|
|
|
|
|
|
//A<><41><EFBFBD><EFBFBD><EFBFBD>ƣ<EFBFBD>ˮƽ<CBAE><C6BD><EFBFBD>ڣ<EFBFBD>
|
2025-03-04 11:54:15 +08:00
|
|
|
|
if (Input.GetKey(KeyCode.A))
|
|
|
|
|
|
{
|
2025-07-02 14:15:36 +08:00
|
|
|
|
Vector3 left = -Vector3.ProjectOnPlane(transform.right, Vector3.up).normalized;
|
|
|
|
|
|
moveDirection += left;
|
2025-03-04 11:54:15 +08:00
|
|
|
|
}
|
2025-07-02 14:15:36 +08:00
|
|
|
|
|
|
|
|
|
|
//D<><44><EFBFBD><EFBFBD><EFBFBD>ƣ<EFBFBD>ˮƽ<CBAE><C6BD><EFBFBD>ڣ<EFBFBD>
|
|
|
|
|
|
if (Input.GetKey(KeyCode.D))
|
2025-03-04 11:54:15 +08:00
|
|
|
|
{
|
2025-07-02 14:15:36 +08:00
|
|
|
|
Vector3 right = Vector3.ProjectOnPlane(transform.right, Vector3.up).normalized;
|
|
|
|
|
|
moveDirection += right;
|
2025-03-04 11:54:15 +08:00
|
|
|
|
}
|
2025-07-02 14:15:36 +08:00
|
|
|
|
|
|
|
|
|
|
// Ӧ<><D3A6><EFBFBD>ƶ<EFBFBD>
|
|
|
|
|
|
if (moveDirection != Vector3.zero)
|
2025-03-04 11:54:15 +08:00
|
|
|
|
{
|
2025-07-02 14:15:36 +08:00
|
|
|
|
ctrlor.Move(moveDirection * moveSpeed * Time.deltaTime);
|
2025-03-04 11:54:15 +08:00
|
|
|
|
}
|
2025-03-19 16:31:17 +08:00
|
|
|
|
#endif
|
2024-12-19 16:38:42 +08:00
|
|
|
|
}
|
2025-07-02 14:15:36 +08:00
|
|
|
|
|
2024-12-19 16:38:42 +08:00
|
|
|
|
if (isRot)
|
|
|
|
|
|
{
|
2024-12-17 14:26:40 +08:00
|
|
|
|
if (Input.GetMouseButtonDown(1))
|
|
|
|
|
|
{
|
|
|
|
|
|
lastMousePosition = Input.mousePosition;
|
|
|
|
|
|
isDragging = true;
|
|
|
|
|
|
SyncRotation();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (Input.GetMouseButtonUp(1))
|
|
|
|
|
|
{
|
|
|
|
|
|
isDragging = false;
|
|
|
|
|
|
}
|
2025-07-02 14:15:36 +08:00
|
|
|
|
|
2024-12-17 14:26:40 +08:00
|
|
|
|
if (isDragging)
|
|
|
|
|
|
{
|
2024-12-19 16:38:42 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת
|
2025-07-02 14:15:36 +08:00
|
|
|
|
Vector3 mouseDelta = Input.mousePosition - lastMousePosition;
|
2024-12-17 14:26:40 +08:00
|
|
|
|
lastMousePosition = Input.mousePosition;
|
|
|
|
|
|
|
2025-07-02 14:15:36 +08:00
|
|
|
|
xRotation -= mouseDelta.y * rotateSpeed;
|
2024-12-17 14:26:40 +08:00
|
|
|
|
yRotation += mouseDelta.x * rotateSpeed;
|
|
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD> X <20><><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>Χ
|
|
|
|
|
|
xRotation = Mathf.Clamp(xRotation, -xRotationLimit, xRotationLimit);
|
|
|
|
|
|
|
|
|
|
|
|
transform.rotation = Quaternion.Euler(xRotation, yRotation, 0);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
public void LookAtPos(Vector3 direction)
|
|
|
|
|
|
{
|
|
|
|
|
|
Quaternion targetRotation = Quaternion.LookRotation(direction);
|
|
|
|
|
|
transform.rotation = targetRotation;
|
|
|
|
|
|
SyncRotation();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-02 14:15:36 +08:00
|
|
|
|
public void Rotate(Vector3 eulerAngles)
|
2024-12-17 14:26:40 +08:00
|
|
|
|
{
|
2025-07-02 14:15:36 +08:00
|
|
|
|
transform.eulerAngles = eulerAngles;
|
2024-12-17 14:26:40 +08:00
|
|
|
|
SyncRotation();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void SyncRotation()
|
|
|
|
|
|
{
|
|
|
|
|
|
Vector3 currentRotation = transform.eulerAngles;
|
|
|
|
|
|
xRotation = currentRotation.x;
|
|
|
|
|
|
yRotation = currentRotation.y;
|
2025-01-13 11:55:37 +08:00
|
|
|
|
|
|
|
|
|
|
// <20><>һ<EFBFBD><D2BB> xRotation <20><> 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);
|
2024-12-17 14:26:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-19 16:38:42 +08:00
|
|
|
|
public void SetLock(bool isMov, bool isRot)
|
2024-12-17 14:26:40 +08:00
|
|
|
|
{
|
2024-12-19 16:38:42 +08:00
|
|
|
|
this.isMov = isMov;
|
|
|
|
|
|
this.isRot = isRot;
|
2024-12-17 14:26:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|