using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.InputSystem;
namespace SoulGames.Utilities
{
public class WASDCameraController : MonoBehaviour
{
[Header("Camera Settings")]
[Tooltip("Camera transform")]
[SerializeField] private Transform cameraTransform;
[Header("Rotation Settings")]
[Tooltip("Camera rotation amount")]
[SerializeField] private float rotationAmount = 2f;
[Tooltip("Camera rotation smooth time")]
[SerializeField] private float rotationLerpTime = 10f;
private Quaternion newRotation;
private Quaternion startRotation;
private bool canRotate;
private bool canRotateX;
private bool canRotateY;
private void Start()
{
startRotation = transform.rotation;
newRotation = transform.rotation;
}
private void Update()
{
HandleMouseInput();
ApplyTransformations();
}
private void HandleMouseInput()
{
// Rotation
if (Input.GetMouseButtonDown(2))
{
canRotate = true;
rotateStartPosition = Input.mousePosition;
}
if (Input.GetMouseButton(2))
{
if (canRotate)
{
if (Input.GetAxis("Mouse X") != 0 && !canRotateY)
{
canRotateX = true;
canRotateY = false;
rotateCurrentPosition = Input.mousePosition;
Vector3 difference = rotateStartPosition - rotateCurrentPosition;
rotateStartPosition = rotateCurrentPosition;
newRotation *= Quaternion.Euler(Vector3.up * (-difference.x / 5f));
Debug.Log("Horizontal");
}
else if (Input.GetAxis("Mouse Y") != 0 && !canRotateX)
{
canRotateX = false;
canRotateY = true;
rotateCurrentPosition = Input.mousePosition;
Vector3 difference = rotateStartPosition - rotateCurrentPosition;
rotateStartPosition = rotateCurrentPosition;
newRotation *= Quaternion.Euler(Vector3.right * (-difference.y / 5f));
Debug.Log("Vertical");
}
}
}
if (Input.GetMouseButtonUp(2))
{
canRotate = false;
canRotateX = false;
canRotateY = false;
}
}
private void ApplyTransformations()
{
// Apply transformations with smoothing
transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, Time.deltaTime * rotationLerpTime);
}
}
}