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>
|
|
|
|
|
|
public float moveSpeed = 5.0f; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><C6B6>ٶ<EFBFBD>
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD>ٶ<EFBFBD>
|
|
|
|
|
|
public float rotateSpeed = 0.05f; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD>ٶ<EFBFBD>
|
|
|
|
|
|
// X<><58><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Χ
|
|
|
|
|
|
public float xRotationLimit = 80.0f;
|
|
|
|
|
|
// Y<><59><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Χ
|
2024-12-17 14:40:30 +08:00
|
|
|
|
//public float yRotationLimit = 180.0f;
|
|
|
|
|
|
//public float minRotationLimitY = -1;
|
|
|
|
|
|
//public float maxRotationLimitY = -1;
|
2024-12-17 14:26:40 +08:00
|
|
|
|
// <20>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ײ<EFBFBD><D7B2><EFBFBD><EFBFBD>
|
|
|
|
|
|
public bool enableCollision = false; // Ĭ<>Ϲر<CFB9><D8B1><EFBFBD>ײ<EFBFBD><D7B2><EFBFBD>⣬<EFBFBD><E2A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
2024-12-17 14:26:40 +08:00
|
|
|
|
private void Awake()
|
|
|
|
|
|
{
|
|
|
|
|
|
instance = this;
|
|
|
|
|
|
DontDestroyOnLoad(this);
|
2025-03-04 11:54:15 +08:00
|
|
|
|
|
|
|
|
|
|
// Cm = GameObject.Find("Mcam");
|
|
|
|
|
|
ctrlor = GetComponent<CharacterController>();
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-12-17 14:26:40 +08:00
|
|
|
|
|
|
|
|
|
|
Global.appSetting.MouseMoveSpeed.RegisterWithInitValue(v => rotateSpeed = v);
|
|
|
|
|
|
}
|
2025-02-18 17:06:53 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>QE<51><45><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
Vector3 GetInputTranslationDirection()
|
|
|
|
|
|
{
|
|
|
|
|
|
Vector3 direction = new Vector3();
|
|
|
|
|
|
|
|
|
|
|
|
if ( Input.GetKey(KeyCode.Q))
|
|
|
|
|
|
{
|
|
|
|
|
|
direction += Vector3.up;
|
|
|
|
|
|
}
|
|
|
|
|
|
if ( Input.GetKey(KeyCode.E))
|
|
|
|
|
|
{
|
|
|
|
|
|
direction += Vector3.down;
|
|
|
|
|
|
}
|
|
|
|
|
|
return direction;
|
|
|
|
|
|
}
|
2024-12-17 14:26:40 +08:00
|
|
|
|
|
|
|
|
|
|
void Update()
|
|
|
|
|
|
{
|
2025-01-13 11:55:37 +08:00
|
|
|
|
if (isMov)
|
2024-12-17 14:26:40 +08:00
|
|
|
|
{
|
2025-03-04 11:54:15 +08:00
|
|
|
|
//// <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-04 11:54:15 +08:00
|
|
|
|
//Vector3 move = transform.right * horizontal + transform.forward * vertical;
|
|
|
|
|
|
|
|
|
|
|
|
//transform.position += move;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//W<><57>ǰ<EFBFBD><C7B0>
|
|
|
|
|
|
if (Input.GetKey(KeyCode.W))
|
|
|
|
|
|
{
|
|
|
|
|
|
Vector3 forward = transform.TransformDirection(Vector3.forward);
|
|
|
|
|
|
ctrlor.Move(forward * moveSpeed * Time.deltaTime);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
//S<><53><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
if (Input.GetKey(KeyCode.S))
|
|
|
|
|
|
{
|
|
|
|
|
|
Vector3 back = transform.TransformDirection(Vector3.back);
|
|
|
|
|
|
ctrlor.Move(back * moveSpeed * Time.deltaTime);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
//A<><41><EFBFBD>ƶ<EFBFBD>
|
|
|
|
|
|
if (Input.GetKey(KeyCode.A))
|
|
|
|
|
|
{
|
|
|
|
|
|
Vector3 left = transform.TransformDirection(Vector3.left);
|
|
|
|
|
|
ctrlor.Move(left * moveSpeed * Time.deltaTime);
|
|
|
|
|
|
}
|
|
|
|
|
|
//D<><44><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
if (Input.GetKey(KeyCode.D) && gameObject.transform.position.y > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
Vector3 right = transform.TransformDirection(Vector3.right);
|
|
|
|
|
|
ctrlor.Move(right * moveSpeed * Time.deltaTime);
|
|
|
|
|
|
}
|
|
|
|
|
|
//E<><45><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
if (Input.GetKey(KeyCode.Q))
|
|
|
|
|
|
{
|
|
|
|
|
|
Vector3 upward = transform.TransformDirection(Vector3.up);
|
|
|
|
|
|
ctrlor.Move(upward * moveSpeed * Time.deltaTime);
|
|
|
|
|
|
}
|
|
|
|
|
|
//E<><45><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
if (Input.GetKey(KeyCode.E))
|
|
|
|
|
|
{
|
|
|
|
|
|
Vector3 down = transform.TransformDirection(Vector3.down);
|
|
|
|
|
|
ctrlor.Move(down * moveSpeed * Time.deltaTime);
|
|
|
|
|
|
}
|
|
|
|
|
|
////<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>QE<51><45><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
//transform.position+= GetInputTranslationDirection()*0.01f;
|
2025-02-18 17:06:53 +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();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-18 17:06:53 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-12-17 14:26:40 +08:00
|
|
|
|
if (Input.GetMouseButtonUp(1))
|
|
|
|
|
|
{
|
|
|
|
|
|
isDragging = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (isDragging)
|
|
|
|
|
|
{
|
2024-12-19 16:38:42 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת
|
|
|
|
|
|
|
2024-12-17 14:26:40 +08:00
|
|
|
|
Vector3 mouseDelta = Input.mousePosition - lastMousePosition; // <20><>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
|
|
|
|
|
|
lastMousePosition = Input.mousePosition;
|
|
|
|
|
|
|
|
|
|
|
|
xRotation -= mouseDelta.y * rotateSpeed; // <20><>ת<EFBFBD><D7AA>X<EFBFBD><58><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD>
|
|
|
|
|
|
yRotation += mouseDelta.x * rotateSpeed;
|
|
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD> X <20><><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>Χ
|
|
|
|
|
|
xRotation = Mathf.Clamp(xRotation, -xRotationLimit, xRotationLimit);
|
|
|
|
|
|
|
2025-01-13 11:55:37 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD> Y <20><><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>Χ
|
|
|
|
|
|
//if (minRotationLimitY != -1 && maxRotationLimitY != -1)
|
2024-12-17 14:26:40 +08:00
|
|
|
|
//{
|
|
|
|
|
|
// yRotation = Mathf.Clamp(yRotation, minRotationLimitY, maxRotationLimitY);
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Rotate(Vector3 eulerAngels)
|
|
|
|
|
|
{
|
|
|
|
|
|
transform.eulerAngles = eulerAngels;
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
}
|