Skip to content

Instantly share code, notes, and snippets.

@aindrigo
Last active August 17, 2023 20:09
Show Gist options
  • Save aindrigo/7b995e5b1df87fec43a959fd1d5f3b23 to your computer and use it in GitHub Desktop.
Save aindrigo/7b995e5b1df87fec43a959fd1d5f3b23 to your computer and use it in GitHub Desktop.
Unity Player Walk
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerWalk : MonoBehaviour
{
// Start is called before the first frame update
[Header("Walking")]
[SerializeField] private float walkSpeed = 10f;
[SerializeField] private float jumpPower = 1f;
[SerializeField] private float grav = -9.81f;
[SerializeField] private CharacterController _controller;
private Vector3 _velocity = Vector3.zero;
[Header("Camera Movement")]
[SerializeField] private float mouseSensitivity = 100f;
[SerializeField] public Transform cameraTransform;
public bool shouldCameraLook = true; // In case another script (like pause menu) wants to disable camera look
private float _xRot = 0f;
private bool isMoving = false;
[Header("Head Bob")]
[SerializeField] private bool doHeadbob = true;
[SerializeField] private float headBobSpeed = 0.15f;
[SerializeField] private float headBobAmt = 0.05f;
private float startY;
private float startX;
private float time;
[Header("Core")] public bool shouldMove = true;
void Start()
{
_controller = GetComponent<CharacterController>();
startY = cameraTransform.localPosition.y;
startX = cameraTransform.localPosition.x;
}
// Update is called once per frame
void HeadbobMovement()
{
if (!_controller.isGrounded || !doHeadbob) return; // Head bob while jumping is stoopid
if (isMoving)
{
time += Mathf.Clamp(Time.deltaTime, 0, 1) + headBobSpeed;
cameraTransform.localPosition = new Vector3(
startX + Mathf.Cos(time * headBobSpeed / 2) * headBobAmt,
startY + Mathf.Sin(time * headBobSpeed) * headBobAmt * 2,
cameraTransform.localPosition.z // Rider stfu nobody asked
);
}
}
void WalkMovement()
{
if (_controller.isGrounded && _velocity.y < 0f)
{
_velocity.y = -2f;
}
float wSpeed = walkSpeed;
if (Input.GetKey(KeyCode.LeftShift))
{
wSpeed += 2f;
}
float x = Input.GetAxis("Horizontal") * wSpeed * Time.deltaTime;
float z = Input.GetAxis("Vertical") * wSpeed * Time.deltaTime;
Vector3 move = new Vector3(x, 0f, z);
if (_controller.isGrounded && Input.GetButtonDown("Jump"))
{
_velocity.y += Mathf.Sqrt(jumpPower * -2f * grav);
}
_velocity.y += grav * Time.deltaTime;
Vector3 moveRes = (_velocity * Time.deltaTime) + transform.TransformDirection(move);
_controller.Move(moveRes);
isMoving = _controller.velocity.magnitude > 0f;
}
void Update()
{
if (!shouldMove)
{
Cursor.lockState = CursorLockMode.None;
return;
}
Cursor.lockState = CursorLockMode.Locked;
CameraMovement();
WalkMovement();
HeadbobMovement();
}
void CameraMovement()
{
if (shouldCameraLook)
Cursor.lockState = CursorLockMode.Locked;
else
{
Cursor.lockState = CursorLockMode.None;
return;
}
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.fixedDeltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.fixedDeltaTime;
_xRot -= mouseY;
_xRot = Mathf.Clamp(_xRot, -90f, 90f);
transform.Rotate(Vector3.up * mouseX);
cameraTransform.localRotation = Quaternion.Euler(_xRot, 0f, 0f);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment