Created
March 31, 2020 15:46
-
-
Save ryanapil/51869356db83e2f962efea55b5db8752 to your computer and use it in GitHub Desktop.
Unity Input System FPS Toggle Perspective (First Person/Third Person)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.InputSystem; | |
public class Player : MonoBehaviour { | |
InputActions controls; | |
public enum Perspective | |
{ | |
firstPerson, thirdPerson | |
} | |
public Perspective perspective = Perspective.firstPerson; | |
public Camera playerCamera; | |
void Awake() | |
{ | |
controls = new InputActions(); | |
controls.Player.Use.performed += context => Use(); | |
controls.Player.Sprint.performed += context => Sprint(); | |
controls.Player.Crouch.performed += context => Crouch(); | |
controls.Player.Jump.performed += context => Jump(); | |
controls.Player.Camera.performed += context => Camera(); | |
} | |
void Camera() | |
{ | |
switch(perspective) | |
{ | |
case Perspective.firstPerson: | |
// Third Person | |
playerCamera.transform.localPosition = new Vector3 (0f, 170f, -315f); | |
perspective = Perspective.thirdPerson; | |
break; | |
case Perspective.thirdPerson: | |
// First Person | |
playerCamera.transform.localPosition = new Vector3(0f, 170f, 14f); | |
perspective = Perspective.firstPerson; | |
break; | |
} | |
Debug.Log("Player Camera"); | |
} | |
void OnEnable() | |
{ | |
controls.Player.Enable(); | |
} | |
void OnDisable() | |
{ | |
controls.Player.Disable(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment