Skip to content

Instantly share code, notes, and snippets.

@ryanapil
Created March 31, 2020 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanapil/51869356db83e2f962efea55b5db8752 to your computer and use it in GitHub Desktop.
Save ryanapil/51869356db83e2f962efea55b5db8752 to your computer and use it in GitHub Desktop.
Unity Input System FPS Toggle Perspective (First Person/Third Person)
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