2D character controller Unity

Creating a 2D character controller Unity involves several steps, including setting up the character’s movement and animation. Below is a step-by-step guide to help you create a simple 2D character controller:

Step 1: Setting Up Your Project

  1. Create a New Project: Open Unity and create a new 2D project.
  2. Import Assets: Import your character sprite and any other assets you’ll need.

Step 2: Creating the Character

  1. Create a Sprite: Drag your character sprite into the scene.
  2. Add a Rigidbody2D: Select your character and add a Rigidbody2D component. Set the Gravity Scale to 0 if you don’t want your character to be affected by gravity.
  3. Add a Collider2D: Add a collider to your character (e.g., BoxCollider2D).

Step 3: Writing the Character Controller Script

  1. Create a Script: In the Project window, create a new C# script and name it CharacterController2D.
  2. Edit the Script: Double-click the script to open it in your code editor and write the following code:
csharpCopy codeusing UnityEngine;

public class CharacterController2D : MonoBehaviour
{
    public float moveSpeed = 5f;
    public Rigidbody2D rb;
    public Animator animator;

    private Vector2 movement;

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        animator = GetComponent<Animator>();
    }

    void Update()
    {
        // Get input from the player
        movement.x = Input.GetAxisRaw("Horizontal");
        movement.y = Input.GetAxisRaw("Vertical");

        // Set animator parameters
        animator.SetFloat("Horizontal", movement.x);
        animator.SetFloat("Vertical", movement.y);
        animator.SetFloat("Speed", movement.sqrMagnitude);
    }

    void FixedUpdate()
    {
        // Move the character
        rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
    }
}

Step 4: Setting Up the Animator

  1. Create Animations: Create animations for your character (e.g., idle, walk).
  2. Create an Animator Controller: Create a new Animator Controller and set up states for each animation.
  3. Assign Parameters: In the Animator window, add float parameters Horizontal, Vertical, and Speed.
  4. Configure Transitions: Set up transitions between animations based on the parameters.

Step 5: Connecting the Script and Animator

  1. Assign the Script: Drag the CharacterController2D script onto your character.
  2. Assign the Animator: In the CharacterController2D script component on your character, assign the Animator component.

Step 6: Testing Your Controller

  1. Play the Game: Press the play button in Unity and test your character’s movement and animations.

Full Script Example

Here is the complete script for the character controller:

csharpCopy codeusing UnityEngine;

public class CharacterController2D : MonoBehaviour
{
    public float moveSpeed = 5f;
    public Rigidbody2D rb;
    public Animator animator;

    private Vector2 movement;

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        animator = GetComponent<Animator>();
    }

    void Update()
    {
        // Get input from the player
        movement.x = Input.GetAxisRaw("Horizontal");
        movement.y = Input.GetAxisRaw("Vertical");

        // Set animator parameters
        animator.SetFloat("Horizontal", movement.x);
        animator.SetFloat("Vertical", movement.y);
        animator.SetFloat("Speed", movement.sqrMagnitude);
    }

    void FixedUpdate()
    {
        // Move the character
        rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
    }
}

Notes:

  • Make sure the Horizontal and Vertical input axes are configured in Unity’s Input Manager.
  • Customize the moveSpeed and other parameters as needed.
  • Ensure that your animations are correctly set up and linked in the Animator Controller.

By following these steps, you should have a basic 2D character controller that handles movement and animation.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top