Skip to content

Instantly share code, notes, and snippets.

@silver-hornet
silver-hornet / 2D Top-Down Character Movement (with RigidBody2D)
Last active February 22, 2021 15:09
2D Top-Down Character Movement (with RigidBody2D)
// This script enables simple, 2D top-down character movement with a RigidBody2D.
// Ensure you have added a RigidBody2D component in the Inspector and have set its Body Type to Dynamic or Kinematic.
// If you're using a Dynamic Body Type, set the Gravity Scale to 0.
// Set the moveSpeed in the Inspector.
// GetAxisRaw makes the movement snappy, so the player instantly stops. If you want smoother movement, use GetAxis instead.
// Applying normalized to movement ensures the player doesn't move faster diagonally.
// If you don't multiply by Time.deltaTime, you will need to use a much smaller float for moveSpeed.
using UnityEngine;