Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binds "I" key to invert mouse look. #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion Assets/Code/FirstPersonController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ public class FirstPersonController : MonoBehaviour {
[Range(1, 100f)]
public float JumpStrength = 2f;

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<CharacterController>();
Expand All @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -77,6 +81,10 @@ public class FirstPersonController : MonoBehaviour {
}
this.cameraTilt = 24;
}

if (Input.GetKeyDown(FirstPersonController.InvertMouseLook)) {
this.invertMouseLook = !this.invertMouseLook;
}
}

public void Enable() {
Expand Down