Tutorial 1 - Project initial setup
Project boilerplate
Creating a core 2D game project and then creating the Scripts, Sprites, Sounds, and Animation folders in order to store the file. Then import the Asset from the Unity Assets Store. The asset we are importing for this project is called FTTGR, use the link below to import the asset to the project.
Import Asset: Click here👉 to open
Player animation - player walk
Slice the player-walk frame using the slicing tool, create the animation, and store it inside the Animation folder. Preview the animation and edit the animation if needed (Play around with the frames and timing).
Tutorial 2 - Main player game object
Player Animation - Player Idle
Select the Idle frame from the Hero Sprite, slice the corresponding frames, right-click, create animation, and store that inside the animation folder.
Making Default Animation
Creating default animation using the animator
Tutorial 3 - Adding unity components
Adding more game objects & component
Create the Ground game object, go to the templates > sprites > environment > Tiles, select any tiles you like and game and drop to the scene, rename it as you want. Then add Box Collider 2D to the object.
What is Box Collider 2D? For assets in a 2D project to have a physical presence in the Scene and be able to interact with each other, they must have a 2D Collider. A 2D Collider is a component that helps define an asset’s physical shape to determine how it will interact with other colliders in a Scene.
After adding the component adjust the game object using the transform tool.
Then select our player game object and add the Box Collider 2D to it adjust the size to fit the game object. Use the resize tool to adjust the outline of the component.
Add the Rigidbody 2D component to the game object. A Rigidbody is a property, that, when added to any object, allows it to interact with a lot of fundamental physics behavior, like forces and acceleration.
Tutorial 4 - Moving the main player using the script
Creating the Script
Create a folder called Player inside the Script folder that is inside the Asset folder, then create a c# script inside the Player folder and name it PlayerMovement.c#. Please review the video to learn more about the code.
PlayerMovement.c#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float speed = 50f;
private Rigidbody2D playerBody;
private Animator anim;
void Awake(){
playerBody = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
void Start()
{
}
void Update()
{
PlayerWalk();
}
void PlayerWalk() {
float move = Input.GetAxisRaw("Horizontal");
if(move>0){
playerBody.velocity = new Vector2(speed, playerBody.velocity.y);
} else if(move <0){
playerBody.velocity = new Vector2(-speed, playerBody.velocity.y);
}
}
}
This script will help the user to control the main player's movement (forward and backward on the x-axis).
Tutorial 5 - Main player movement animation transition
Creating Animation Transition
- To create a transition between animations, go to Animator and create an int (data type) parameter called "Speed".
🤔 What is Animator? An Animator Controller allows you to arrange and maintain a set of Animation Clips and associated Animation Transitions for a character or object.
- After creating the Speed parameter set the conditions (transition condition), and click on the inspector tab to view these functions. The condition that we have to implement: if Speed is greater than 0 then set the transition from PlayerIdle to PlayerAnimation, else the Speed is 0 then the transition should be set from PlayerAnimation to PlayerIdle.
*note* it is better to disable/uncheck the transition duration to avoid the transition lag (these fields are inside the settings inside the inspector tab).
- How can we access the "Speed" (case-sensitive) parameter in the script?
It is straightforward to access the parameter because we have already initialized the Animator (in the last tutorial section) called anim, and we are getting the animator component in the void Awake () function (see the previous lecture for reference).
Controlling the transition parameter
- After initializing the animator, set the parameter :
//pass the parameter name& value
anim.setInteger("Speed", Mathf.abs((int)playerBody.velocity.x));
Pass the parameter name (case sensitive) followed by the value, the value should be an integer so we have to cast the return value from the player velocity.
Tutorial 6 - Fixing Main Player Movements & Transition
Eliminating the sliding/skating movement of the Main Player
The main player sliding happens due to the condition flaw in the PlayerWalk() function. In the PlayerWalk function, it doesn't state what would be done when the move value equals to 0.
Void PlayerWalk();
void PlayerWalk()
{
float move = Input.GetAxisRaw("Horizontal");
if (move > 0)
{
playerBody.velocity = new Vector2(speed, playerBody.velocity.y);
}
else if (move < 0)
{
playerBody.velocity = new Vector2(-speed, playerBody.velocity.y);
}
else
{
playerBody.velocity = new Vector2(0f, playerBody.velocity.y);
}
//Controlling the Speed paremeter of the Palyer
anim.SetInteger("Speed", Mathf.Abs((int)playerBody.velocity.x));
}
Stopping the delayed animation of the Main Player
Go to Animator > Select the Transition > uncheck the Has Exit Time box, and do the same configuration to both the transitions this will result in the execute the perfect timing for the animation.
Tutorial 7 - Fixing Main Player Movements & Transition
Video Coming Soon
What is a Prefab?
It's a special type of component that allows fully configured GameObjects to be saved in the Project for reuse. These assets can then be shared between scenes, or even other projects without having to be configured again. This is quite useful for objects that will be used many times, such as platforms.