free rotate camera

This commit is contained in:
2026-05-13 21:01:50 -05:00
parent aee3fe9ea7
commit 8fef9c25d7
10 changed files with 262 additions and 46 deletions
+50
View File
@@ -0,0 +1,50 @@
using UnityEngine;
public static class DebugUtilities
{
/// <summary>
/// Draws a wire sphere using Debug.DrawLine for runtime visibility.
/// </summary>
public static void DrawWireSphere(Vector3 center, float radius, Color color, float duration, int quality = 3)
{
quality = Mathf.Clamp(quality, 1, 10);
int segments = quality << 2;
int subdivisions = quality << 3;
int halfSegments = segments >> 1;
float strideAngle = 360f / subdivisions;
float segmentStride = 180f / segments;
Vector3 first;
Vector3 next;
// Draw meridians (vertical circles)
for (int i = 0; i < segments; i++)
{
first = (Vector3.forward * radius);
first = Quaternion.AngleAxis(segmentStride * (i - halfSegments), Vector3.right) * first;
for (int j = 0; j < subdivisions; j++)
{
next = Quaternion.AngleAxis(strideAngle, Vector3.up) * first;
Debug.DrawLine(first + center, next + center, color, duration);
first = next;
}
}
// Draw parallels (horizontal circles)
Vector3 axis;
for (int i = 0; i < segments; i++)
{
first = (Vector3.forward * radius);
first = Quaternion.AngleAxis(segmentStride * (i - halfSegments), Vector3.up) * first;
axis = Quaternion.AngleAxis(90f, Vector3.up) * first;
for (int j = 0; j < subdivisions; j++)
{
next = Quaternion.AngleAxis(strideAngle, axis) * first;
Debug.DrawLine(first + center, next + center, color, duration);
first = next;
}
}
}
}
+2
View File
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 933aa2a82d546964cb7e1a8e7a1e007f
+12 -3
View File
@@ -1547,7 +1547,7 @@ Transform:
serializedVersion: 2
m_LocalRotation: {x: 0.30070576, y: -0, z: -0, w: 0.953717}
m_LocalPosition: {x: -0.35901004, y: 47.8, z: -70.2}
m_LocalScale: {x: 1, y: 0.78067374, z: 1.6579802}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children:
- {fileID: 6872729306491234319}
@@ -2147,13 +2147,13 @@ Transform:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 5784121484016646142}
serializedVersion: 2
m_LocalRotation: {x: -0, y: -0.000000028422978, z: 0.000000008961729, w: 1}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 2119885955527004040}
m_LocalEulerAnglesHint: {x: 35, y: 0, z: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!20 &7814557473141052265
Camera:
m_ObjectHideFlags: 0
@@ -3155,6 +3155,15 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 34a2af7866b6ecc4ab5f2b6222f7dbe2, type: 3}
m_Name:
m_EditorClassIdentifier: Assembly-CSharp::CameraController
MainCamera: {fileID: 7814557473141052265}
mouseSensitivity: 1000
minPitch: 10
maxPitch: 60
distance: 6
heightOffset: -0.5
followSpeed: 1000
maxZoom: 6
minZoom: 1
playerCamHome: {fileID: 2119885955527004040}
--- !u!114 &5412473048133157928
MonoBehaviour:
+68 -14
View File
@@ -1822,6 +1822,11 @@ Transform:
m_Children: []
m_Father: {fileID: 265036979}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!20 &509486404 stripped
Camera:
m_CorrespondingSourceObject: {fileID: 7814557473141052265, guid: 0fd9b22e9158e474a96c42de5ee0d85f, type: 3}
m_PrefabInstance: {fileID: 7959480261809233915}
m_PrefabAsset: {fileID: 0}
--- !u!1 &562959765
GameObject:
m_ObjectHideFlags: 0
@@ -3610,17 +3615,6 @@ RectTransform:
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0, y: 0}
--- !u!114 &1733921841 stripped
MonoBehaviour:
m_CorrespondingSourceObject: {fileID: 5478143440182735446, guid: 0fd9b22e9158e474a96c42de5ee0d85f, type: 3}
m_PrefabInstance: {fileID: 7959480261809233915}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 34a2af7866b6ecc4ab5f2b6222f7dbe2, type: 3}
m_Name:
m_EditorClassIdentifier: Assembly-CSharp::CameraController
--- !u!1 &1735546792
GameObject:
m_ObjectHideFlags: 0
@@ -4379,10 +4373,30 @@ PrefabInstance:
serializedVersion: 3
m_TransformParent: {fileID: 1704696510}
m_Modifications:
- target: {fileID: 818899427131401759, guid: 6d1ef2b53d3ef0446852da522af66b73, type: 3}
propertyPath: m_AnchorMax.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 1376587328550839904, guid: 6d1ef2b53d3ef0446852da522af66b73, type: 3}
propertyPath: m_Name
value: UI
objectReference: {fileID: 0}
- target: {fileID: 2975696880262954876, guid: 6d1ef2b53d3ef0446852da522af66b73, type: 3}
propertyPath: m_AnchorMax.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4264548731807291060, guid: 6d1ef2b53d3ef0446852da522af66b73, type: 3}
propertyPath: m_AnchorMax.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 5101319162034548862, guid: 6d1ef2b53d3ef0446852da522af66b73, type: 3}
propertyPath: m_AnchorMax.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 5514935285560916770, guid: 6d1ef2b53d3ef0446852da522af66b73, type: 3}
propertyPath: m_AnchorMax.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 6792308726050395253, guid: 6d1ef2b53d3ef0446852da522af66b73, type: 3}
propertyPath: m_LocalPosition.x
value: 0
@@ -4423,6 +4437,10 @@ PrefabInstance:
propertyPath: m_LocalEulerAnglesHint.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 7535213278904715570, guid: 6d1ef2b53d3ef0446852da522af66b73, type: 3}
propertyPath: m_AnchorMax.y
value: 0
objectReference: {fileID: 0}
m_RemovedComponents: []
m_RemovedGameObjects: []
m_AddedGameObjects: []
@@ -4441,10 +4459,30 @@ PrefabInstance:
serializedVersion: 3
m_TransformParent: {fileID: 0}
m_Modifications:
- target: {fileID: 1480974628599415149, guid: 0fd9b22e9158e474a96c42de5ee0d85f, type: 3}
propertyPath: cameraController
- target: {fileID: 2119885955527004040, guid: 0fd9b22e9158e474a96c42de5ee0d85f, type: 3}
propertyPath: m_LocalScale.y
value: 1
objectReference: {fileID: 0}
- target: {fileID: 2119885955527004040, guid: 0fd9b22e9158e474a96c42de5ee0d85f, type: 3}
propertyPath: m_LocalScale.z
value: 1
objectReference: {fileID: 0}
- target: {fileID: 5478143440182735446, guid: 0fd9b22e9158e474a96c42de5ee0d85f, type: 3}
propertyPath: minPitch
value: 10
objectReference: {fileID: 0}
- target: {fileID: 5478143440182735446, guid: 0fd9b22e9158e474a96c42de5ee0d85f, type: 3}
propertyPath: MainCamera
value:
objectReference: {fileID: 1733921841}
objectReference: {fileID: 509486404}
- target: {fileID: 5478143440182735446, guid: 0fd9b22e9158e474a96c42de5ee0d85f, type: 3}
propertyPath: followSpeed
value: 1000
objectReference: {fileID: 0}
- target: {fileID: 5478143440182735446, guid: 0fd9b22e9158e474a96c42de5ee0d85f, type: 3}
propertyPath: mouseSensitivity
value: 1000
objectReference: {fileID: 0}
- target: {fileID: 5526037850913171920, guid: 0fd9b22e9158e474a96c42de5ee0d85f, type: 3}
propertyPath: m_LocalPosition.x
value: -0.60945
@@ -4485,6 +4523,22 @@ PrefabInstance:
propertyPath: m_LocalEulerAnglesHint.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 6872729306491234319, guid: 0fd9b22e9158e474a96c42de5ee0d85f, type: 3}
propertyPath: m_LocalRotation.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 6872729306491234319, guid: 0fd9b22e9158e474a96c42de5ee0d85f, type: 3}
propertyPath: m_LocalRotation.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 6872729306491234319, guid: 0fd9b22e9158e474a96c42de5ee0d85f, type: 3}
propertyPath: m_LocalRotation.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 6872729306491234319, guid: 0fd9b22e9158e474a96c42de5ee0d85f, type: 3}
propertyPath: m_LocalEulerAnglesHint.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 9054701043111961555, guid: 0fd9b22e9158e474a96c42de5ee0d85f, type: 3}
propertyPath: m_Name
value: Doug
+87 -5
View File
@@ -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,7 +60,9 @@ public void RotateCam()
/// <summary>
/// Set cam rotation immediately
/// </summary>
public void SnapToRotation(float rotation)
void SnapToRotation(float rotation)
{
if (CamControlMode == CameraControlMode.SnapRotate)
{
playerCamHome.RotateAround(
transform.position,
@@ -41,16 +72,15 @@ public void SnapToRotation(float 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
}
+1 -12
View File
@@ -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;
}
+6 -2
View File
@@ -342,6 +342,9 @@ MonoBehaviour:
skyOcclusionIntensityMultiplier:
m_OverrideState: 1
m_Value: 1
worldOffset:
m_OverrideState: 1
m_Value: {x: 0, y: 0, z: 0}
--- !u!114 &-1216621516061285780
MonoBehaviour:
m_ObjectHideFlags: 3
@@ -376,6 +379,9 @@ MonoBehaviour:
highQualityFiltering:
m_OverrideState: 1
m_Value: 0
filter:
m_OverrideState: 1
m_Value: 0
downscale:
m_OverrideState: 1
m_Value: 0
@@ -462,8 +468,6 @@ MonoBehaviour:
- {fileID: -6288072647309666549}
- {fileID: 7518938298396184218}
- {fileID: -1410297666881709256}
- {fileID: -7750755424749557576}
- {fileID: -5139089513906902183}
--- !u!114 &853819529557874667
MonoBehaviour:
m_ObjectHideFlags: 3
+1
View File
@@ -1,6 +1,7 @@
{
"dependencies": {
"com.unity.ai.navigation": "2.0.9",
"com.unity.cinemachine": "3.1.6",
"com.unity.collab-proxy": "2.10.2",
"com.unity.ide.visualstudio": "2.0.25",
"com.unity.render-pipelines.universal": "17.3.0",
+28
View File
@@ -19,6 +19,16 @@
},
"url": "https://packages.unity.com"
},
"com.unity.cinemachine": {
"version": "3.1.6",
"depth": 0,
"source": "registry",
"dependencies": {
"com.unity.splines": "2.0.0",
"com.unity.modules.imgui": "1.0.0"
},
"url": "https://packages.unity.com"
},
"com.unity.collab-proxy": {
"version": "2.10.2",
"depth": 0,
@@ -107,6 +117,13 @@
"dependencies": {},
"url": "https://packages.unity.com"
},
"com.unity.settings-manager": {
"version": "2.1.1",
"depth": 2,
"source": "registry",
"dependencies": {},
"url": "https://packages.unity.com"
},
"com.unity.shadergraph": {
"version": "17.3.0",
"depth": 1,
@@ -116,6 +133,17 @@
"com.unity.searcher": "4.9.3"
}
},
"com.unity.splines": {
"version": "2.8.2",
"depth": 1,
"source": "registry",
"dependencies": {
"com.unity.mathematics": "1.2.1",
"com.unity.modules.imgui": "1.0.0",
"com.unity.settings-manager": "1.0.3"
},
"url": "https://packages.unity.com"
},
"com.unity.test-framework": {
"version": "1.6.0",
"depth": 1,