diff --git a/Assets/FirstPersonController/Scripts/MouseLook.cs b/Assets/FirstPersonController/Scripts/MouseLook.cs index 3e4caf6..a051aca 100644 --- a/Assets/FirstPersonController/Scripts/MouseLook.cs +++ b/Assets/FirstPersonController/Scripts/MouseLook.cs @@ -1,37 +1,55 @@ /****************************************************************************** * Initial code based on Brackey's FPS video * https://youtu.be/_QajrabyTJc + * + * Notes: + * In the video, he attaches this script to the camera. I refactored it to put + * it up on the main player element. That way the behaviors are all in one + * place and "hidden." + * + * Changed the mouse sensitivity variable to use a Float Reference. This allows + * me to put the value into a property/settings UI so the player can change the + * value to his/her preference. Or it can be changed at design time in Unity. *****************************************************************************/ -using System.Collections; -using System.Collections.Generic; +/****************************************************************************** + * The Mouse Look script is responsible for rotating the player object and the + * main camera in response to the movement of the mouse. + * + * Movement of the mouse on the Y axis simulates looking up and down. This will + * rotate the camera and is clamped to a human-like range of motion. + * + * Movement on the mouse on the X axis simulates turning. This will rotate the + * player object. + *****************************************************************************/ +using Architecture.Variables; using UnityEngine; public class MouseLook : MonoBehaviour { - public float mouseSensitivity = 100f; - public Transform playerBody; - + [SerializeField] private FloatReference mouseSensitivity; private float xRotation = 0f; - // Start is called before the first frame update + [SerializeField] private Transform mainCamera; + void Start() { // Keep the mouse cursor in the game window Cursor.lockState = CursorLockMode.Locked; } - // Update is called once per frame void Update() { - float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime; - float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime; + float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity.Value * Time.deltaTime; + float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity.Value * Time.deltaTime; + // Simulate looking up and down by rotating the camera. Clamp the rotation + // to limit the range of motion. xRotation -= mouseY; xRotation = Mathf.Clamp(xRotation, -90f, 90f); - transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f); - - playerBody.Rotate(Vector3.up * mouseX); + mainCamera.localRotation = Quaternion.Euler(xRotation, 0f, 0f); + // Simulate turning + transform.Rotate(Vector3.up * mouseX); } } diff --git a/Assets/FirstPersonController/Variables.meta b/Assets/FirstPersonController/Variables.meta new file mode 100644 index 0000000..0081159 --- /dev/null +++ b/Assets/FirstPersonController/Variables.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 84535e060457e3e488de47035efa7eb0 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/FirstPersonController/Variables/MouseSensitivity.asset b/Assets/FirstPersonController/Variables/MouseSensitivity.asset new file mode 100644 index 0000000..dff8dc7 --- /dev/null +++ b/Assets/FirstPersonController/Variables/MouseSensitivity.asset @@ -0,0 +1,16 @@ +%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: 7cb18c6bb3c4bcd40b8451100e11b136, type: 3} + m_Name: MouseSensitivity + m_EditorClassIdentifier: + DeveloperDescription: + Value: 250 diff --git a/Assets/FirstPersonController/Variables/MouseSensitivity.asset.meta b/Assets/FirstPersonController/Variables/MouseSensitivity.asset.meta new file mode 100644 index 0000000..3c33b6e --- /dev/null +++ b/Assets/FirstPersonController/Variables/MouseSensitivity.asset.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 32c605729eb4c1246bcc95c94a8608e4 +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 0 + userData: + assetBundleName: + assetBundleVariant: