Skip to content

Instantly share code, notes, and snippets.

@imAliAsad
Last active September 6, 2021 17:58
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 imAliAsad/14402cf5be4354be56a3e5c258d2bd62 to your computer and use it in GitHub Desktop.
Save imAliAsad/14402cf5be4354be56a3e5c258d2bd62 to your computer and use it in GitHub Desktop.
Use gamepad controller to fly free camera in gameview. This is the improved and customized script of Unity's ExtendedFlycam script
using System;
using UnityEngine;
using System.Collections;
public class GamepadFlycam : MonoBehaviour
{
public float cameraSensitivity = 90;
public float climbSpeed = 4;
public float normalMoveSpeed = 10;
public float slowMoveFactor = 0.25f;
public float fastMoveFactor = 3;
private float rotationX = 0.0f;
private float rotationY = 0.0f;
void Start()
{
Screen.lockCursor = true;
}
void Update()
{
//Duplicate 'Mouse X', name it 'Joy X' from InputManager and change Type -> Joystick Axis
//Axis -> 4th axis (joystick)
//Sensitivity = 1
rotationX += Input.GetAxis("Joy X") * cameraSensitivity * Time.deltaTime;
//Duplicate 'Mouse Y', name it 'Joy Y' from InputManager and change Type -> Joystick Axis
//Axis -> 5th axis (joystick)
//Sensitivity = 1
//Invert = true (check)
rotationY += Input.GetAxis("Joy Y") * cameraSensitivity * Time.deltaTime;
rotationY = Mathf.Clamp(rotationY, -90, 90);
transform.localRotation = Quaternion.AngleAxis(rotationX, Vector3.up);
transform.localRotation *= Quaternion.AngleAxis(rotationY, Vector3.left);
//if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
if (Input.GetKey(KeyCode.Joystick1Button0))
{
transform.position += transform.forward * (normalMoveSpeed * fastMoveFactor) * Input.GetAxis("Vertical") * Time.deltaTime;
transform.position += transform.right * (normalMoveSpeed * fastMoveFactor) * Input.GetAxis("Horizontal") * Time.deltaTime;
}
else if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl))
{
transform.position += transform.forward * (normalMoveSpeed * slowMoveFactor) * Input.GetAxis("Vertical") * Time.deltaTime;
transform.position += transform.right * (normalMoveSpeed * slowMoveFactor) * Input.GetAxis("Horizontal") * Time.deltaTime;
}
else
{
transform.position += transform.forward * normalMoveSpeed * Input.GetAxis("Vertical") * Time.deltaTime;
transform.position += transform.right * normalMoveSpeed * Input.GetAxis("Horizontal") * Time.deltaTime;
}
if (Input.GetKey(KeyCode.Joystick1Button5)) { transform.position += transform.up * climbSpeed * Time.deltaTime; }
if (Input.GetKey(KeyCode.Joystick1Button4)) { transform.position -= transform.up * climbSpeed * Time.deltaTime; }
if (Input.GetKeyDown(KeyCode.End))
{
Screen.lockCursor = (Screen.lockCursor == false) ? true : false;
}
}
}
@JAMS49
Copy link

JAMS49 commented Sep 6, 2021

Please put colliding on your flycam

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment