Skip to content

Instantly share code, notes, and snippets.

@lordlycastle
Created June 12, 2020 19:46
Show Gist options
  • Save lordlycastle/055dff80f7559262624c3240b0182aca to your computer and use it in GitHub Desktop.
Save lordlycastle/055dff80f7559262624c3240b0182aca to your computer and use it in GitHub Desktop.
Fly in the world with simple PC controls. Can't get simpler than this.
using UnityEngine;
namespace WFShaderDemo {
public class FreeFlyCamera : MonoBehaviour {
public bool lockCursor = false;
public float cameraSensitivity = 4;
public float normalMoveSpeed = 10;
public float smoothTime = 10f;
public Camera cam;
private float rotationX = 0.0f;
private float rotationY = 0.0f;
void Start() {
if (lockCursor) {
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
}
void Update() {
var dt = Mathf.Clamp(Time.deltaTime, 0f, 0.03f);
rotationX += Input.GetAxis("Mouse X") * cameraSensitivity;
rotationY += Input.GetAxis("Mouse Y") * cameraSensitivity;
rotationY = Mathf.Clamp(rotationY, -90, 90);
if (rotationX > 360) rotationX -= 360f;
var locRot = Quaternion.AngleAxis(rotationX, Vector3.up);
locRot *= Quaternion.AngleAxis(rotationY, Vector3.left);
transform.localRotation = Quaternion.Slerp(transform.localRotation,
locRot, smoothTime * dt);
float speed = normalMoveSpeed * dt;
if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
speed *= 4;
var deltaVertical = transform.forward;
var deltaHorizontal = transform.right;
if (Input.GetKey(KeyCode.Q)) { transform.position += Vector3.up * speed; }
if (Input.GetKey(KeyCode.E)) { transform.position -= Vector3.up * speed; }
transform.position += deltaVertical * speed * Input.GetAxis("Vertical");
transform.position += deltaHorizontal * speed * Input.GetAxis("Horizontal");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment