Skip to content

Instantly share code, notes, and snippets.

@MarcelvanDuijnDev
Created January 8, 2022 14:31
Show Gist options
  • Save MarcelvanDuijnDev/539811f86e16ac6a684d09bc41967c7e to your computer and use it in GitHub Desktop.
Save MarcelvanDuijnDev/539811f86e16ac6a684d09bc41967c7e to your computer and use it in GitHub Desktop.
Unity Camera Movement - FreeCam
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement_FreeCamera : MonoBehaviour
{
[SerializeField] private float _Speed = 5;
[SerializeField] private float _SprintSpeed = 8;
private float _CurrentSpeed;
void Start()
{
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
if (Input.GetKey(KeyCode.LeftShift))
_CurrentSpeed = _SprintSpeed;
else
_CurrentSpeed = _Speed;
float xas = Input.GetAxis("Horizontal");
float zas = Input.GetAxis("Vertical");
transform.Translate(new Vector3(xas,0, zas) * _CurrentSpeed * Time.deltaTime);
float mousex = Input.GetAxis("Mouse X");
float mousey = Input.GetAxis("Mouse Y");
transform.eulerAngles += new Vector3(-mousey, mousex, 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment