How do I play an animation when jumping, and then play another one when falling, which can both be interrupted by things like landing?
So I've been trying to implement jumping into my game. I got down the jumping itself, but I'm struggling with the implementation. I'm also completely new to programming btw. I tried to use a ground check to determine if the animation should be played or not, but that just meant that it would play the jumping animation for a split second, but then the idle/running animation again since the ground check was positive. This is a result of the ground check happening too fast. Then I tried using a trigger, but then realised that I don't know a way of cancelling the animation after it happened. I would prefer to have an animation for going upwards, and one for going downwards, like in the title. Anyone got a solution for me?
Answer by tormentoarmagedoom · May 02, 2019 at 10:05 AM
Good day.
This is one of the main fucntions of Unity Animations; the control of when they commence, if they merge, if they stop, etc..
You should spend some time looking some youtube tutorial about this. Dont be afraidod spending some hours learning how to correctly control animations. If you spend just 2-3 hours learning with attention, you will be a super pro!!
Good luck!
Answer by Jack-Mariani · May 02, 2019 at 12:41 PM
There are quite many options to implement this (assuming you're using a rigidbody2d).
Consider that we have 2 elements here.
The animator used to implement the right animations.
The Rigidbody2D to check where the player is moving.
Basically, I think you've some transition on your animator that is not properly set.
I would set an order of priorities like this.
if player is not moving => idle
if player is moving && he is on the ground => run
if player is not on the ground and moving up => jump
if player is not on the ground and moving down => fall
With the animator we consider that each state could lead to any of the other states. So we will have something like this.
Each arrow will be just a trigger to a specific state.
After that you may use this code to send the right trigger at the right time.
[RequireComponent(typeof(Rigidbody2D), typeof(Animator))]
public class Rigidbody2DAnimations : MonoBehaviour
{
//to decide the minimal speed
public float speedThreshold = 0.1f;
//the rigidbody
private Rigidbody2D _rigidbody2D;
private Animator _animator;
//cache the rigidbody at start (or awake)
private void Start()
{
_rigidbody2D = GetComponent<Rigidbody2D>();
_animator = GetComponent<Animator>();
}
//I think the best place to handle animations is on LateUpdate
private void LateUpdate()
{
//gets the velocity as a vector
var velocity = _rigidbody2D.velocity;
// --------------- IDLE --------------- //
//if the current speed is lower than the treshold you can set animator is idle
if (velocity.magnitude <= speedThreshold)
{
_animator.SetTrigger("Idle");
return;
}
// --------------- FACING --------------- //
//apply here any logic related to move left or right
if (velocity.x > 0) FaceRight();
else FaceLeft();
// --------------- ON GROUND --------------- //
//if we are moving on the ground
if (IsOnTheGround())
_animator.SetTrigger("Running");
// --------------- JUMPING --------------- //
else if (!IsOnTheGround() &&
velocity.y > 0)
_animator.SetTrigger("Jumping");
// --------------- FALLING --------------- //
else if (!IsOnTheGround() &&
velocity.y < 0)
_animator.SetTrigger("Falling");
}
//ground check logic here
private bool IsOnTheGround() { return true; }
}
You may remove the falling animation and just replace the code with this:
else if (!IsOnTheGround())
_animator.SetTrigger("Jumping");
This code just show you the logic and it's quite simple (there are too many calls of IsOnTheGround(), you may just keep the first one.
I'm being verbose just to let you understand the logic.
I suggest you to optimize both code and transitions if you want a more complex animation system.
Further info:
Your answer
Follow this Question
Related Questions
How do i extend a animation ? 1 Answer
Involve Instatiated objects in Animation 1 Answer
Get when animator change state 0 Answers
Making animations for separate body parts 2D pixel art 1 Answer
Help with 8-DOM Weapon Sprite Issue 0 Answers