Part of these game systems:
2D Player Controller
Complete 2D character controller with smooth movement, variable-height jumping, and coyote time.
How to Use
1
Attach to your player GameObject
2
Add a Rigidbody2D component
3
Create an empty child object at the feet for Ground Check
4
Set the Ground Layer in the inspector
5
Configure movement and jump values to your liking
Source Code
C#
using UnityEngine;
[RequireComponent(typeof(Rigidbody2D))]
public class PlayerController2D : MonoBehaviour
{
[Header("Movement")]
[SerializeField] private float moveSpeed = 8f;
[SerializeField] private float acceleration = 60f;
[SerializeField] private float deceleration = 50f;
[Header("Jumping")]
[SerializeField] private float jumpForce = 16f;
[SerializeField] private float coyoteTime = 0.15f;
[SerializeField] private float jumpBufferTime = 0.1f;
[Header("Ground Check")]
[SerializeField] private Transform groundCheck;
[SerializeField] private float groundCheckRadius = 0.2f;
[SerializeField] private LayerMask groundLayer;
private Rigidbody2D rb;
private float moveInput;
private bool isGrounded;
private float coyoteTimer;
private float jumpBufferTimer;
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
rb.freezeRotation = true;
}
private void Update()
{
moveInput = Input.GetAxisRaw("Horizontal");
// Ground check
isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, groundLayer);
// Coyote time
if (isGrounded)
coyoteTimer = coyoteTime;
else
coyoteTimer -= Time.deltaTime;
// Jump buffer
if (Input.GetButtonDown("Jump"))
jumpBufferTimer = jumpBufferTime;
else
jumpBufferTimer -= Time.deltaTime;
// Jump
if (jumpBufferTimer > 0f && coyoteTimer > 0f)
{
rb.linearVelocity = new Vector2(rb.linearVelocity.x, jumpForce);
jumpBufferTimer = 0f;
coyoteTimer = 0f;
}
// Variable jump height
if (Input.GetButtonUp("Jump") && rb.linearVelocity.y > 0f)
{
rb.linearVelocity = new Vector2(rb.linearVelocity.x, rb.linearVelocity.y * 0.5f);
}
}
private void FixedUpdate()
{
float targetSpeed = moveInput * moveSpeed;
float speedDiff = targetSpeed - rb.linearVelocity.x;
float accelRate = Mathf.Abs(targetSpeed) > 0.01f ? acceleration : deceleration;
float movement = speedDiff * accelRate * Time.fixedDeltaTime;
rb.AddForce(Vector2.right * movement, ForceMode2D.Force);
}
private void OnDrawGizmosSelected()
{
if (groundCheck != null)
{
Gizmos.color = Color.green;
Gizmos.DrawWireSphere(groundCheck.position, groundCheckRadius);
}
}
}