free rotate camera
This commit is contained in:
@@ -2,17 +2,46 @@
|
||||
using System.Linq;
|
||||
using Unity.VisualScripting;
|
||||
using UnityEngine;
|
||||
using static UnityEngine.GraphicsBuffer;
|
||||
|
||||
public class CameraController : MonoBehaviour
|
||||
{
|
||||
public float camTargetRotation { get; private set; } = 0;
|
||||
public CameraControlMode CamControlMode { get; private set; } = CameraControlMode.SnapRotate;
|
||||
public Camera MainCamera;
|
||||
|
||||
private bool isCamRotating = false;
|
||||
|
||||
|
||||
[Header("Free Cam Settings")]
|
||||
public float mouseSensitivity = 200f;
|
||||
public float minPitch = -30f;
|
||||
public float maxPitch = 60f;
|
||||
public float distance = 6f;
|
||||
public float heightOffset = -0.5f;
|
||||
public float followSpeed = 10f;
|
||||
public float maxZoom = 6f;
|
||||
public float minZoom = 1f;
|
||||
private float zoom = 2.3f;
|
||||
private float yaw;
|
||||
private float pitch;
|
||||
|
||||
public Transform playerCamHome;
|
||||
Transform dougBody;
|
||||
|
||||
private void Update()
|
||||
{
|
||||
HandleCamRotation();
|
||||
if (Input.GetMouseButton(1))
|
||||
{
|
||||
HandleFreeCamRotation();
|
||||
}
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
Vector3 angles = transform.eulerAngles;
|
||||
|
||||
yaw = angles.y;
|
||||
pitch = angles.x;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -31,26 +60,27 @@ public void RotateCam()
|
||||
/// <summary>
|
||||
/// Set cam rotation immediately
|
||||
/// </summary>
|
||||
public void SnapToRotation(float rotation)
|
||||
void SnapToRotation(float rotation)
|
||||
{
|
||||
playerCamHome.RotateAround(
|
||||
transform.position,
|
||||
Vector3.up,
|
||||
rotation
|
||||
);
|
||||
if (CamControlMode == CameraControlMode.SnapRotate)
|
||||
{
|
||||
playerCamHome.RotateAround(
|
||||
transform.position,
|
||||
Vector3.up,
|
||||
rotation
|
||||
);
|
||||
|
||||
camTargetRotation = rotation;
|
||||
camTargetRotation = rotation;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Move cam to desired rotation (every frame)
|
||||
/// </summary>
|
||||
void HandleCamRotation()
|
||||
void HandleSnapCamRotation()
|
||||
{
|
||||
float currentY = playerCamHome.transform.rotation.eulerAngles.y;
|
||||
|
||||
float step = 200 * Time.deltaTime;
|
||||
|
||||
float newY = Mathf.MoveTowardsAngle(currentY, camTargetRotation, step);
|
||||
|
||||
playerCamHome.RotateAround(
|
||||
@@ -64,4 +94,56 @@ void HandleCamRotation()
|
||||
isCamRotating = false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Move cam to desired free rotation (every frame)
|
||||
/// </summary>
|
||||
void HandleFreeCamRotation()
|
||||
{
|
||||
if (dougBody == null)
|
||||
{
|
||||
dougBody = GameManager.Instance.PlayerController.DougBody.transform;
|
||||
}
|
||||
|
||||
Vector3 target = new Vector3(dougBody.position.x, dougBody.position.y + heightOffset, dougBody.position.z);
|
||||
|
||||
// Mouse Input
|
||||
float mouseX = Input.GetAxis("Mouse X");
|
||||
float mouseY = Input.GetAxis("Mouse Y");
|
||||
|
||||
yaw += mouseX * mouseSensitivity * Time.deltaTime;
|
||||
pitch -= mouseY * mouseSensitivity * Time.deltaTime;
|
||||
|
||||
pitch = Mathf.Clamp(pitch, minPitch, maxPitch);
|
||||
|
||||
// Zoom
|
||||
float scroll = Input.GetAxis("Mouse ScrollWheel");
|
||||
|
||||
zoom -= scroll * 2f;
|
||||
zoom = Mathf.Clamp(zoom, minZoom, maxZoom);
|
||||
MainCamera.orthographicSize = zoom;
|
||||
|
||||
// Rotation
|
||||
Quaternion rotation = Quaternion.Euler(pitch, yaw, 0);
|
||||
|
||||
// Desired Position
|
||||
Vector3 offset = rotation * Vector3.forward * -distance;
|
||||
Vector3 desiredPosition = target + offset;
|
||||
|
||||
// Smooth Follow
|
||||
playerCamHome.transform.position = Vector3.Lerp(
|
||||
playerCamHome.transform.position,
|
||||
desiredPosition,
|
||||
followSpeed * Time.deltaTime
|
||||
);
|
||||
|
||||
// Look at player
|
||||
playerCamHome.transform.LookAt(target + Vector3.up);
|
||||
}
|
||||
}
|
||||
|
||||
public enum CameraControlMode
|
||||
{
|
||||
SnapRotate,
|
||||
FreeRotate
|
||||
}
|
||||
|
||||
@@ -47,6 +47,7 @@ void Update()
|
||||
{
|
||||
if (!paused)
|
||||
{
|
||||
Cursor.lockState = CursorLockMode.None;
|
||||
Time.timeScale = 0;
|
||||
canUseTools = false;
|
||||
hasControl = false;
|
||||
@@ -73,18 +74,6 @@ void Update()
|
||||
|
||||
CheckWeaponChange();
|
||||
ApplyWalk();
|
||||
|
||||
// TODO: This is just for testing, remove or clean up if used
|
||||
if (Input.GetKeyDown(KeyCode.Q))
|
||||
{
|
||||
cameraController.RotateCam();
|
||||
}
|
||||
|
||||
// TODO: Add actual save feature later, this is just for test
|
||||
if (Input.GetKeyDown(KeyCode.I))
|
||||
{
|
||||
SaveSystem.Save();
|
||||
}
|
||||
}
|
||||
|
||||
if (canUseTools)
|
||||
|
||||
@@ -10,7 +10,6 @@ public class PlayerManager : MonoBehaviour
|
||||
public void SavePlayerData(ref PlayerSaveData data)
|
||||
{
|
||||
data.activeToolIndex = GameManager.Instance.PlayerManager.CurrentToolIndex;
|
||||
data.camRotation = GameManager.Instance.PlayerController.cameraController.camTargetRotation;
|
||||
data.dougPosition = GameManager.Instance.PlayerController.transform.position;
|
||||
data.currentScene = SceneManager.GetActiveScene().name;
|
||||
}
|
||||
@@ -24,7 +23,6 @@ public void LoadPlayerData(PlayerSaveData data)
|
||||
else
|
||||
{
|
||||
GameManager.Instance.PlayerManager.CurrentToolIndex = data.activeToolIndex;
|
||||
GameManager.Instance.PlayerController.cameraController.SnapToRotation(data.camRotation);
|
||||
GameManager.Instance.PlayerController.transform.position = data.dougPosition;
|
||||
}
|
||||
}
|
||||
@@ -35,7 +33,6 @@ public void LoadPlayerData(PlayerSaveData data)
|
||||
public struct PlayerSaveData
|
||||
{
|
||||
public int activeToolIndex;
|
||||
public float camRotation;
|
||||
public Vector3 dougPosition;
|
||||
public string currentScene;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user