Skip to content

Instantly share code, notes, and snippets.

@whoisryosuke
Last active November 18, 2021 22:24
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 whoisryosuke/28be9309bfc3682c68cb21802c38c3e5 to your computer and use it in GitHub Desktop.
Save whoisryosuke/28be9309bfc3682c68cb21802c38c3e5 to your computer and use it in GitHub Desktop.
Unity - Basic 3D/FPS Character and Camera Movement + Jumping - based on Brackeyes video: https://www.youtube.com/watch?v=_QajrabyTJc
  1. Create a new game object PlayerControllerFPS.
  2. Create a empty game object child under PlayerControllerFPS called GroundCheck.
  3. Drag the MainCamera under the PlayerControllerFPS. Reset the local positioning of the camera back to 0,0,0 if needed.
  4. Attach the PlayerMovement script to the PlayerControllerFPS.
  5. Drag the CharacterController into the properties of the PlayerMovement script.
  6. Drag the GroundCheck over to the GroundCheck property.
  7. Attach the MouseLook to the MainCamera.
  8. Drag the PlayerControllerFPS from your scene onto the playerBody property.
  9. You should have character movement and rotation!
  10. Press escape to stop the camera movement. Ideally you can enable a debug menu based on this.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseLook : MonoBehaviour
{
private bool mouseLock = true;
public float mouseSensitivity = 100f;
public Transform playerBody;
float xRotation = 0f;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
// Check if we disable lock state
if(Input.GetButtonDown("Cancel"))
{
if(mouseLock == true)
{
Debug.Log("Unlocking cursor");
mouseLock = false;
Cursor.lockState = CursorLockMode.None;
} else
{
Debug.Log("Locking cursor");
mouseLock = true;
Cursor.lockState = CursorLockMode.Locked;
}
}
// Only move mouse when cursor is locked
// For menus and overlays to prevent movement
if(mouseLock == true)
{
// Handle movement based on mouse
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerBody.Rotate(Vector3.up * mouseX);
}
}
}
using UnityEngine;
[RequireComponent(typeof(CharacterController))]
public class PlayerMovement : MonoBehaviour
{
public CharacterController controller;
public float speed = 12f;
public float gravity = -9.81f;
public float groundDistance = 0.4f;
public float jumpHeight = 3f;
public Transform groundCheck;
Vector3 velocity;
bool isGrounded;
public LayerMask groundMask;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
if (Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment