Hello! Today, we will take a closer look at how to implement a character controller while developing a 2D game with Unity. We will write scripts for the movement, jumping, and animation transitions of our 2D character. This tutorial is designed for those who are familiar with the basics of using Unity, so it assumes that users are comfortable with the Unity interface.
1. Project Setup
First, create a new 2D project in Unity. Please set it up as follows:
- Project Name: 2DCharacterController
- Template: 2D
Once the project is created, import the necessary assets. We will create a character using basic 2D sprites. We need the following sprites:
- Character sprites (idle, run, jump)
- Background sprite
Add these sprites to the Assets folder.
2. Setting Up the Scene
Place the sprites that will be used in the scene. Use the Sprite Renderer to add the background and character to the scene. Select Create > 2D Object > Sprite in the hierarchy to create a new game object, and then set the sprite.
3. Writing the Character Controller Script
Now it’s time to write a script to control the character. Create a new C# script in the Assets folder and name it PlayerController. This script will handle the character’s movement, jumping, and animation transitions.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed = 5f;
public float jumpForce = 10f;
public bool isGrounded;
private Rigidbody2D rb;
private Animator animator;
void Start()
{
rb = GetComponent();
animator = GetComponent();
}
void Update()
{
Move();
Jump();
Animate();
}
private void Move()
{
float moveInput = Input.GetAxis("Horizontal");
rb.velocity = new Vector2(moveInput * moveSpeed, rb.velocity.y);
}
private void Jump()
{
if (Input.GetButtonDown("Jump") && isGrounded)
{
rb.AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse);
}
}
private void Animate()
{
float moveInput = Input.GetAxis("Horizontal");
animator.SetFloat("Speed", Mathf.Abs(moveInput));
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Ground"))
{
isGrounded = true;
}
}
private void OnCollisionExit2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Ground"))
{
isGrounded = false;
}
}
}
4. Adding Animations
To add animations to the character, we will use the Animator component. First, you need to create an Animator Controller. Right-click in the Assets folder, select Create > Animator Controller, and then drag it onto the Character object to add it.
Double-click the Animator Controller to open the Animator window and add the following animations:
- Idle
- Run
- Jump
Connect each animation appropriately and set the transition conditions. For example, you can set it so that the Idle and Run animations transition based on the Speed parameter.
5. Testing the Actual Game
Now that everything is set up, let’s click the Play button in the Unity editor to test the game. Check if the character moves left and right, jumps, and transitions between animations correctly.
6. Implementing Additional Features
In addition to basic character movement and jumping, you can add various features to enrich the game. For example:
- Double jump
- Add attack animations and functionalities
- Implement an item collection system
Each feature can be added in a similar manner as the basic movement and jumping functionalities. You just need to write new methods and call them in the Update() function.
Conclusion
Today, we learned how to implement a character controller for 2D games in Unity. I hope you understood the relationship between movement, jumping, and animation transitions. Based on this tutorial, feel free to add more features and develop your own game. Game development is a world of endless creativity!
Now it’s your turn to implement your ideas. If you have any questions about using Unity, feel free to ask anytime. Have fun and enjoy the exciting world of game development!