Question by
PandaNinja17 · Dec 08, 2019 at 04:27 AM ·
animation2d game
Force Quit Animation and Start New One
I'm making a 2D platformer and am working on my animations. Right now I have a jump animation that is a little long (in case I jump from a very high platform to a very low platform). I want to cut off the jump/fall animation when I hit the ground and immediately transition to the idle or walk animation. How do I do it? Here's the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
// walking variables
private Rigidbody2D rb;
public float speed;
public float jumpForce;
private float moveInput;
// jumping variables
private bool isGrounded;
public Transform feetPos;
public float checkRadius;
public LayerMask whatIsGround;
private Animator anim;
private void Start()
{
// get animation
anim = GetComponent<Animator>();
// get the Rigidbody
rb = GetComponent<Rigidbody2D>();
}
private void FixedUpdate()
{
// move left + right
moveInput = Input.GetAxisRaw("Horizontal");
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
}
private void Update()
{
// check if on ground
isGrounded = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGround);
// idle animation
if (moveInput == 0)
{
anim.SetBool("isRunning", false);
}
else
{
anim.SetBool("isRunning", true);
}
// flip left + right
if(moveInput > 0)
{
transform.eulerAngles = new Vector3(0, 0, 0);
}
else if(moveInput < 0)
{
transform.eulerAngles = new Vector3(0, 180, 0);
}
// jump
if(isGrounded == true && Input.GetKeyDown(KeyCode.Space))
{
anim.SetBool("isJumping", true);
rb.velocity = Vector2.up * jumpForce;
}
else
{
anim.SetBool("isJumping", false);
}
}
}
Here's the animator:
Thanks in advance.
unityhelp.png
(53.6 kB)
Comment
Your answer
Follow this Question
Related Questions
How can I stop my character from walking during the attack animation? 1 Answer
2D Animation Feet Pivot Point 0 Answers
My game Object is bigger when runninng the game than in edit view 1 Answer
Shooting animation and bullet generation issue 0 Answers
Why isn't my jumping animation working? 0 Answers