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
- Create a New Project: Open Unity and create a new 2D project.
- Import Assets: Import your character sprite and any other assets you’ll need.
Step 2: Creating the Character
- Create a Sprite: Drag your character sprite into the scene.
- Add a Rigidbody2D: Select your character and add a
Rigidbody2D
component. Set theGravity Scale
to0
if you don’t want your character to be affected by gravity. - Add a Collider2D: Add a collider to your character (e.g.,
BoxCollider2D
).
Step 3: Writing the Character Controller Script
- Create a Script: In the Project window, create a new C# script and name it
CharacterController2D
. - 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
- Create Animations: Create animations for your character (e.g., idle, walk).
- Create an Animator Controller: Create a new Animator Controller and set up states for each animation.
- Assign Parameters: In the Animator window, add float parameters
Horizontal
,Vertical
, andSpeed
. - Configure Transitions: Set up transitions between animations based on the parameters.
Step 5: Connecting the Script and Animator
- Assign the Script: Drag the
CharacterController2D
script onto your character. - Assign the Animator: In the
CharacterController2D
script component on your character, assign the Animator component.
Step 6: Testing Your Controller
- 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
andVertical
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.