Skip to content

Instantly share code, notes, and snippets.

@MarGamaDev
Created January 23, 2024 12:34
Show Gist options
  • Save MarGamaDev/10cef9141fe7a4b04030d9475ab1bcf7 to your computer and use it in GitHub Desktop.
Save MarGamaDev/10cef9141fe7a4b04030d9475ab1bcf7 to your computer and use it in GitHub Desktop.
using System.Collections;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.InputSystem;
[RequireComponent(typeof(Rigidbody))]
public class PlayerController : MonoBehaviour
{
public Rigidbody RigidBody => rigidBody;
private Rigidbody rigidBody;
private Vector2 movementInput = new Vector2();
public Vector2 MovementInput { get { return movementInput; } private set { } }
[SerializeField] private float moveForce;
public float MoveForce { get { return moveForce; } }
[Space]
[SerializeField] private float jumpForce;
[SerializeField] private float floorRayLength;
[SerializeField] private LayerMask groundLayer;
private bool isJumping = false;
[Space]
[SerializeField] private float dashForce;
public float DashForce { get { return dashForce; } }
[SerializeField] private float dashCooldown;
[SerializeField] private UnityEvent onDash;
private bool isDashing;
private Coroutine dashCooldownCoroutine;
public float DashCooldown { get { return dashCooldown; } }
[SerializeField] private bool isAlive = true;
public bool IsAlive { get { return isAlive;} set { isAlive = value; } }
public int PlayerNumber { get; set; }
[SerializeField] private AnimatedHamster animatedHamster;
public AnimatedHamster AnimatedHamster => animatedHamster;
[SerializeField] private LockRotation lockRotation;
public LockRotation LockRotation => lockRotation;
private Vector3 bufferedVelocity;
public Vector3 BufferedVelocity { get { return bufferedVelocity; } }
private void Awake()
{
DontDestroyOnLoad(gameObject);
rigidBody = GetComponent<Rigidbody>();
}
private void Start()
{
StartCoroutine(DoBufferVelocity());
}
public void OnMove(InputAction.CallbackContext context)
{
movementInput = context.ReadValue<Vector2>();
}
public void OnJump(InputAction.CallbackContext context)
{
isJumping = context.action.triggered;
}
public void OnDash(InputAction.CallbackContext context)
{
isDashing = context.action.triggered;
}
private void FixedUpdate()
{
if (!isAlive)
{
// Attempt at fixing the jump bug that can occur at the start of the game
isJumping = false;
isDashing = false;
return;
}
// move
Vector3 moveForceVector = new Vector3(movementInput.x, 0, movementInput.y) * moveForce;
rigidBody.AddForce(moveForceVector);
// dash
if (isDashing && dashCooldownCoroutine == null && movementInput != Vector2.zero)
{
Vector3 dashForceVector = new Vector3(movementInput.x, 0, movementInput.y) * dashForce;
rigidBody.AddForce(dashForceVector, ForceMode.Impulse);
onDash?.Invoke();
dashCooldownCoroutine = StartCoroutine(DoDashCooldown());
}
// jump
bool isGrounded = Physics.Raycast(transform.position, Vector3.down, floorRayLength, groundLayer);
if (isJumping && isGrounded)
{
// bouncy material workaround to advoid super jumps.
if (rigidBody.velocity.y / rigidBody.mass < jumpForce)
{
rigidBody.velocity = new Vector3(rigidBody.velocity.x, 0, rigidBody.velocity.z);
rigidBody.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
}
}
private void OnDrawGizmosSelected()
{
Gizmos.DrawLine(transform.position, transform.position + Vector3.down * floorRayLength);
}
private IEnumerator DoDashCooldown()
{
yield return new WaitForSeconds(dashCooldown);
dashCooldownCoroutine = null;
}
private IEnumerator DoBufferVelocity()
{
Vector3 _currentVelocity = rigidBody.velocity;
while (true)
{
yield return new WaitForFixedUpdate();
bufferedVelocity = _currentVelocity;
_currentVelocity = rigidBody.velocity;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment