step-by-step guide to writing your first C# (pronounced “C Sharp) movement script — from project setup to grounded jumps. No prior experience needed.
One of the most satisfying moments in game development is pressing Play and watching your character move for the very first time. If you are just getting started with Unity, smooth 2D movement is the first real milestone — and its much more simpler than most people expect.
In this guide you will build a 2D player character from scratch, write a movement and jump script in C#, and add a ground check so your player cannot jump endlessly in the air. Every line of code is explained. Let’s build.
- Step 1 — Set Up Your Unity Project
- Step 2 — Create the Player Object
- Step 3 — Add Physics Components
- Step 4 — Write the Movement Script
- Step 5 — Add Jumping
- Step 6 — Ground Check (No Infinite Jumps)
- Quick Reference Summary
Step 1 — Set Up Your Project
Open Unity Hub, click New Project, and select the 2D template. Name it something like MyPlatformer, choose a save location, and click Create.
Why the 2D template?It pre-configures your camera, render pipeline, and default physics for 2D — saving several manual setup steps that trip up beginners.
Step 2 — Create the Player Object
Import a sprite by dragging any image into the Project Panel. A solid-color square works perfectly for learning. Then drag it into the Scene View — Unity automatically creates a GameObject with a Sprite Renderer. Rename it Player in the Hierarchy.
Create a Ground object too Drag another sprite into the scene, name it
Ground, and position it below the player. You will need it to test collisions in Step 3.
Step 3 — Add Physics Components
Unity uses components to give objects behaviors. Your player needs exactly two:
🏃Rigidbody 2D
Enables real physics — gravity, velocity, momentum. After adding: set Gravity Scale to 1, and enable Freeze Rotation Z so the character doesn’t tip sideways.
📦Box Collider 2D
Defines the physical boundary of the player. Also add this to your Ground object so the player can actually land on it.
To add: select Player in the Hierarchy → Add Component in the Inspector → search each name and click.
Step 4 — Write the Movement Script
Right-click in the Project Panel → Create → C# Script → name it PlayerMovement. Double-click to open, replace everything with this:
I don’t know how to write code on WordPress. That’s all I can do. Work with it. I will improve next time.
Save it, then drag the script from the Project Panel onto the Player in the Hierarchy. Press Play — left/right movement should work!
📌Why FixedUpdate and not Update?Update() runs every frame (variable speed). FixedUpdate() runs at a fixed physics rate — essential for consistent, frame-rate-independent movement.
Step 5 — Add Jumping
Update PlayerMovement.cs — add a jumpForce variable and Space key detection inside Update():
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 5f;
public float jumpForce = 10f; // <-- new
private Rigidbody2D rb;
private float moveInput;
void Start() { rb = GetComponent<Rigidbody2D>(); }
void Update()
{
moveInput = Input.GetAxisRaw("Horizontal");
// Jump on Space press
if (Input.GetKeyDown(KeyCode.Space))
{
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
}
}
void FixedUpdate()
{
rb.velocity = new Vector2(moveInput * moveSpeed, rb.velocity.y);
}
}
Your character should now move and jump! Now One problem remains: the player can jump infinitely while airborne. Step 6 solves that.
Step 6 — Ground Check
A ground check prevents mid-air jumping by verifying the player is touching the floor before a jump is allowed. It is the most commonly skipped step — and the most noticeable bug.
6a. Create a Ground Layer
Select your Ground object → Inspector → Layer dropdown → Add Layer → type Ground. Go back and assign that layer to the Ground object.
6b. Add a GroundCheck Child Object
In the Hierarchy, right-click Player → Create Empty → rename it GroundCheck. In the Scene View, position it just below the player’s feet — this is the detection point.
6c. Final Script
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 5f;
public float jumpForce = 10f;
// Assign in Inspector
public Transform groundCheck;
public float checkRadius = 0.2f;
public LayerMask groundLayer;
private Rigidbody2D rb;
private float moveInput;
private bool isGrounded;
void Start() { rb = GetComponent<Rigidbody2D>(); }
void Update()
{
// Tiny circle at player's feet — returns true if touching Ground layer
isGrounded = Physics2D.OverlapCircle(
groundCheck.position,
checkRadius,
groundLayer
);
moveInput = Input.GetAxisRaw("Horizontal");
// Jump only when grounded
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
}
}
void FixedUpdate()
{
rb.velocity = new Vector2(moveInput * moveSpeed, rb.velocity.y);
}
}
its so hard to write script on WordPress. If there’s problem by copying script. Than you have to write manual. Sorry for that.
Select Player in the Inspector — drag GroundCheck into the Ground Check slot, and set Ground Layer to the Ground layer you created.
⚠️Don’t forget to assign the layer!If Ground Layer is empty, isGrounded is always false — your player won’t be able to jump at all.