From 2b50266843f205a8777bc83a847161356c47b878 Mon Sep 17 00:00:00 2001 From: Matt Roberts Date: Mon, 14 Jan 2019 20:08:23 -0800 Subject: [PATCH 1/2] Binds "I" key to invert mouse look. --- Assets/Code/FirstPersonController.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Assets/Code/FirstPersonController.cs b/Assets/Code/FirstPersonController.cs index 2e8d40a..4681c6d 100644 --- a/Assets/Code/FirstPersonController.cs +++ b/Assets/Code/FirstPersonController.cs @@ -14,13 +14,16 @@ public class FirstPersonController : MonoBehaviour { [Range(1, 100f)] public float JumpStrength = 2f; - private CharacterController characterController; + public const KeyCode InvertMouseLook = KeyCode.I; + + private CharacterController characterController; private Transform cameraTransform; private float cameraTilt = 0f; private float verticalSpeed = 0f; private float timeInAir = 0f; private bool jumpLocked = false; + private bool invertMouseLook = false; void Start () { this.characterController = this.GetComponent(); @@ -33,6 +36,7 @@ public class FirstPersonController : MonoBehaviour { bool touchesGround = this.onGround(); float runMultiplier = 1f + 2f * Input.GetAxis("Run"); float y = this.transform.position.y; + float mouseY = Input.GetAxis("Look Y") * (this.invertMouseLook ? -1 : 1); Vector3 movementVector = this.transform.forward * Input.GetAxis("Move Y") + this.transform.right * Input.GetAxis("Move X"); if (movementVector.sqrMagnitude > 1) { // this check prevents partial joystick input from becoming 100% speed movementVector.Normalize(); // this prevents diagonal movement form being too fast @@ -43,7 +47,7 @@ public class FirstPersonController : MonoBehaviour { this.transform.position += Vector3.down * verticalMovement; } this.transform.rotation = Quaternion.AngleAxis(Input.GetAxis("Look X") * Time.deltaTime * this.LookSensitivity, Vector3.up) * this.transform.rotation; - this.cameraTilt = Mathf.Clamp(this.cameraTilt - Input.GetAxis("Look Y") * this.LookSensitivity * Time.deltaTime, -90f, 90f); + this.cameraTilt = Mathf.Clamp(this.cameraTilt - mouseY * this.LookSensitivity * Time.deltaTime, -90f, 90f); this.cameraTransform.localRotation = Quaternion.AngleAxis(this.cameraTilt, Vector3.right); if (touchesGround) { @@ -77,6 +81,10 @@ public class FirstPersonController : MonoBehaviour { } this.cameraTilt = 24; } + + if (Input.GetKeyDown(FirstPersonController.InvertMouseLook)) { + this.invertMouseLook = !this.invertMouseLook; + } } public void Enable() { From 61a1d4889ccf31208d230007d3203412c0ca575f Mon Sep 17 00:00:00 2001 From: Matt Roberts Date: Tue, 15 Jan 2019 13:36:17 -0800 Subject: [PATCH 2/2] Fixes tabs. Incorrectly assumed editor would handle tabs. --- Assets/Code/FirstPersonController.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Assets/Code/FirstPersonController.cs b/Assets/Code/FirstPersonController.cs index 4681c6d..0b472f5 100644 --- a/Assets/Code/FirstPersonController.cs +++ b/Assets/Code/FirstPersonController.cs @@ -14,16 +14,16 @@ public class FirstPersonController : MonoBehaviour { [Range(1, 100f)] public float JumpStrength = 2f; - public const KeyCode InvertMouseLook = KeyCode.I; + public const KeyCode InvertMouseLook = KeyCode.I; - private CharacterController characterController; + private CharacterController characterController; private Transform cameraTransform; private float cameraTilt = 0f; private float verticalSpeed = 0f; private float timeInAir = 0f; private bool jumpLocked = false; - private bool invertMouseLook = false; + private bool invertMouseLook = false; void Start () { this.characterController = this.GetComponent(); @@ -36,7 +36,7 @@ public class FirstPersonController : MonoBehaviour { bool touchesGround = this.onGround(); float runMultiplier = 1f + 2f * Input.GetAxis("Run"); float y = this.transform.position.y; - float mouseY = Input.GetAxis("Look Y") * (this.invertMouseLook ? -1 : 1); + float mouseY = Input.GetAxis("Look Y") * (this.invertMouseLook ? -1 : 1); Vector3 movementVector = this.transform.forward * Input.GetAxis("Move Y") + this.transform.right * Input.GetAxis("Move X"); if (movementVector.sqrMagnitude > 1) { // this check prevents partial joystick input from becoming 100% speed movementVector.Normalize(); // this prevents diagonal movement form being too fast @@ -82,9 +82,9 @@ public class FirstPersonController : MonoBehaviour { this.cameraTilt = 24; } - if (Input.GetKeyDown(FirstPersonController.InvertMouseLook)) { - this.invertMouseLook = !this.invertMouseLook; - } + if (Input.GetKeyDown(FirstPersonController.InvertMouseLook)) { + this.invertMouseLook = !this.invertMouseLook; + } } public void Enable() {