From 7fefaae4050eda3007e63a8569f6c4811d4133ff Mon Sep 17 00:00:00 2001 From: cyndrdev Date: Wed, 27 Jan 2021 12:06:17 +0000 Subject: [PATCH] basic camera controller --- game/Assets/Scenes/Main.unity | 17 +++++++- game/Assets/Scripts/Camera.meta | 8 ++++ .../Assets/Scripts/Camera/CameraController.cs | 42 +++++++++++++++++++ .../Scripts/Camera/CameraController.cs.meta | 11 +++++ game/Assets/Scripts/Camera/CameraSettings.cs | 16 +++++++ .../Scripts/Camera/CameraSettings.cs.meta | 11 +++++ .../Scripts/Input/CameraRelativeInput.cs | 23 ++++++++++ .../Scripts/Input/CameraRelativeInput.cs.meta | 11 +++++ .../Assets/Scripts/Player/PlayerController.cs | 2 +- .../Scripts/Player/PlayerInputHandler.cs | 10 +++-- game/Assets/Settings/Camera.meta | 8 ++++ .../Settings/Camera/CameraSettings.asset | 18 ++++++++ .../Settings/Camera/CameraSettings.asset.meta | 8 ++++ .../Input/PlayerControls.inputactions | 2 +- 14 files changed, 181 insertions(+), 6 deletions(-) create mode 100644 game/Assets/Scripts/Camera.meta create mode 100644 game/Assets/Scripts/Camera/CameraController.cs create mode 100644 game/Assets/Scripts/Camera/CameraController.cs.meta create mode 100644 game/Assets/Scripts/Camera/CameraSettings.cs create mode 100644 game/Assets/Scripts/Camera/CameraSettings.cs.meta create mode 100644 game/Assets/Scripts/Input/CameraRelativeInput.cs create mode 100644 game/Assets/Scripts/Input/CameraRelativeInput.cs.meta create mode 100644 game/Assets/Settings/Camera.meta create mode 100644 game/Assets/Settings/Camera/CameraSettings.asset create mode 100644 game/Assets/Settings/Camera/CameraSettings.asset.meta diff --git a/game/Assets/Scenes/Main.unity b/game/Assets/Scenes/Main.unity index f3cb315..a1cce59 100644 --- a/game/Assets/Scenes/Main.unity +++ b/game/Assets/Scenes/Main.unity @@ -706,7 +706,7 @@ MonoBehaviour: m_ActionId: 2bbc2e93-6016-4544-8399-0c4ee222850c m_ActionName: CoreMap/Use[/Keyboard/e,/XInputControllerWindows/buttonWest] m_NeverAutoSwitchControlSchemes: 0 - m_DefaultControlScheme: PCMR + m_DefaultControlScheme: m_DefaultActionMap: CoreMap m_SplitScreenIndex: -1 m_Camera: {fileID: 1444606338} @@ -751,6 +751,7 @@ GameObject: - component: {fileID: 1444606338} - component: {fileID: 1444606337} - component: {fileID: 1444606340} + - component: {fileID: 1444606341} m_Layer: 0 m_Name: PlayerCamera m_TagString: MainCamera @@ -855,6 +856,20 @@ MonoBehaviour: m_RequiresDepthTexture: 0 m_RequiresColorTexture: 0 m_Version: 2 +--- !u!114 &1444606341 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1444606336} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 805caadbe4df94a40bf7ddb70bea02f6, type: 3} + m_Name: + m_EditorClassIdentifier: + _settings: {fileID: 11400000, guid: 995f378ab762cd344b7a6d108f049191, type: 2} + _inputHandler: {fileID: 1292675913} --- !u!1 &1468685575 GameObject: m_ObjectHideFlags: 0 diff --git a/game/Assets/Scripts/Camera.meta b/game/Assets/Scripts/Camera.meta new file mode 100644 index 0000000..5b25ea4 --- /dev/null +++ b/game/Assets/Scripts/Camera.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: c3040315fc41e344b95af9bba0d16776 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/game/Assets/Scripts/Camera/CameraController.cs b/game/Assets/Scripts/Camera/CameraController.cs new file mode 100644 index 0000000..33755e0 --- /dev/null +++ b/game/Assets/Scripts/Camera/CameraController.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class CameraController : MonoBehaviour +{ + [SerializeField] + private CameraSettings _settings; + + [SerializeField] + private PlayerInputHandler _inputHandler; + + private Vector2 _cameraRotation; + + void FixedUpdate() + { + float deltaTime = Time.fixedDeltaTime; + + _cameraRotation += _inputHandler.InputState.Look * ( deltaTime * _settings.LookSpeed ); + _cameraRotation.y = Mathf.Clamp( _cameraRotation.y, _settings.LookYMin, _settings.LookYMax ); + _inputHandler.SetCameraRotation(_cameraRotation.x * -180.0f); + } + + private void LateUpdate() + { + if ( _cameraRotation.x > 1.0f ) + { + _cameraRotation.x -= 2.0f; + } + else if ( _cameraRotation.x < -1.0f ) + { + _cameraRotation.x += 2.0f; + } + + Quaternion rot = Quaternion.Euler( _cameraRotation.y * 180.0f, _cameraRotation.x * 180.0f, 0f ); + Vector3 pos = rot * ( Vector3.back * _settings.LookDistance ); + + transform.localPosition = pos; + transform.rotation = rot; + } +} \ No newline at end of file diff --git a/game/Assets/Scripts/Camera/CameraController.cs.meta b/game/Assets/Scripts/Camera/CameraController.cs.meta new file mode 100644 index 0000000..e8e23e4 --- /dev/null +++ b/game/Assets/Scripts/Camera/CameraController.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 805caadbe4df94a40bf7ddb70bea02f6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/game/Assets/Scripts/Camera/CameraSettings.cs b/game/Assets/Scripts/Camera/CameraSettings.cs new file mode 100644 index 0000000..67e84a9 --- /dev/null +++ b/game/Assets/Scripts/Camera/CameraSettings.cs @@ -0,0 +1,16 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +[CreateAssetMenu(fileName = "CameraSettings.asset", menuName = "KernelPanic/Camera/Settings")] +public class CameraSettings : ScriptableObject +{ + [Header("Speed")] + public float LookSpeed; + + [Header("Constraints")] + public float LookDistance; + + public float LookYMin; + public float LookYMax; +} diff --git a/game/Assets/Scripts/Camera/CameraSettings.cs.meta b/game/Assets/Scripts/Camera/CameraSettings.cs.meta new file mode 100644 index 0000000..5581b7c --- /dev/null +++ b/game/Assets/Scripts/Camera/CameraSettings.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b67d3eff4427cf14182daa0548676571 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/game/Assets/Scripts/Input/CameraRelativeInput.cs b/game/Assets/Scripts/Input/CameraRelativeInput.cs new file mode 100644 index 0000000..589a202 --- /dev/null +++ b/game/Assets/Scripts/Input/CameraRelativeInput.cs @@ -0,0 +1,23 @@ +using System.Collections; +using System.Collections.Generic; +using TMPro; +using UnityEngine; + +public struct CameraRelativeInput +{ + private Vector2 _base; + private float _cameraAngle; + + public void SetValue( Vector2 value ) + { + _base = value; + } + + public void SetAngle( float angle ) + { + _cameraAngle = angle; + } + + public Vector2 Value + => Quaternion.Euler( 0f, 0f, _cameraAngle ) * _base; +} diff --git a/game/Assets/Scripts/Input/CameraRelativeInput.cs.meta b/game/Assets/Scripts/Input/CameraRelativeInput.cs.meta new file mode 100644 index 0000000..fcc3473 --- /dev/null +++ b/game/Assets/Scripts/Input/CameraRelativeInput.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 2001265c10e110042a86e193cff6f419 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/game/Assets/Scripts/Player/PlayerController.cs b/game/Assets/Scripts/Player/PlayerController.cs index 0a36bac..97e4774 100644 --- a/game/Assets/Scripts/Player/PlayerController.cs +++ b/game/Assets/Scripts/Player/PlayerController.cs @@ -20,7 +20,7 @@ public class PlayerController : MonoBehaviour private void FixedUpdate() { - Vector2 moveVector2 = _inputHandler.InputState.Move; + Vector2 moveVector2 = _inputHandler.InputState.Move.Value; Vector3 moveVector = new Vector3( moveVector2.x, 0f, moveVector2.y ); _rigidbody.velocity = moveVector * _movementSettings.BaseMovementSpeed; diff --git a/game/Assets/Scripts/Player/PlayerInputHandler.cs b/game/Assets/Scripts/Player/PlayerInputHandler.cs index 7f53f03..ef4997d 100644 --- a/game/Assets/Scripts/Player/PlayerInputHandler.cs +++ b/game/Assets/Scripts/Player/PlayerInputHandler.cs @@ -13,8 +13,9 @@ public class PlayerInputHandler : MonoBehaviour { public BufferedInput Jump; public BufferedInput Use; - public Vector2 Move; + public CameraRelativeInput Move; public Vector2 Look; + public float MoveRotation; } public PlayerInputState InputState => _state; @@ -27,8 +28,8 @@ public class PlayerInputHandler : MonoBehaviour _state.Jump = new BufferedInput( _inputSettings.JumpBufferTime ); _state.Use = new BufferedInput( _inputSettings.UseBufferTime ); + _state.Move = new CameraRelativeInput(); _state.Look = Vector2.zero; - _state.Move = Vector2.zero; } private void FixedUpdate() @@ -39,11 +40,14 @@ public class PlayerInputHandler : MonoBehaviour _state.Use.Update( deltaTime ); } + public void SetCameraRotation( float angle ) + => _state.Move.SetAngle( angle ); + public void Look( InputAction.CallbackContext context ) => _state.Look = context.ReadValue(); public void Move( InputAction.CallbackContext context ) - => _state.Move = context.ReadValue(); + => _state.Move.SetValue( context.ReadValue() ); public void Jump( InputAction.CallbackContext context ) => _state.Jump.Set( context.ReadValue() > 0.5f ); diff --git a/game/Assets/Settings/Camera.meta b/game/Assets/Settings/Camera.meta new file mode 100644 index 0000000..a094777 --- /dev/null +++ b/game/Assets/Settings/Camera.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 4b0a99647895c3d45bf1200c49d99d31 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/game/Assets/Settings/Camera/CameraSettings.asset b/game/Assets/Settings/Camera/CameraSettings.asset new file mode 100644 index 0000000..a306833 --- /dev/null +++ b/game/Assets/Settings/Camera/CameraSettings.asset @@ -0,0 +1,18 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!114 &11400000 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: b67d3eff4427cf14182daa0548676571, type: 3} + m_Name: CameraSettings + m_EditorClassIdentifier: + LookSpeed: 1 + LookDistance: 8 + LookYMin: 0.025 + LookYMax: 0.5 diff --git a/game/Assets/Settings/Camera/CameraSettings.asset.meta b/game/Assets/Settings/Camera/CameraSettings.asset.meta new file mode 100644 index 0000000..7739f1a --- /dev/null +++ b/game/Assets/Settings/Camera/CameraSettings.asset.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 995f378ab762cd344b7a6d108f049191 +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 11400000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/game/Assets/Settings/Input/PlayerControls.inputactions b/game/Assets/Settings/Input/PlayerControls.inputactions index 97a35ab..8c10c35 100644 --- a/game/Assets/Settings/Input/PlayerControls.inputactions +++ b/game/Assets/Settings/Input/PlayerControls.inputactions @@ -110,7 +110,7 @@ "id": "f11a0fcc-85b6-41ab-adbb-e9630b5bfdaa", "path": "/delta", "interactions": "", - "processors": "", + "processors": "ScaleVector2(x=0.1,y=0.1)", "groups": "PCMR", "action": "Look", "isComposite": false,