So I have created a run animation and a jump animation and an idle animation that can all transition bewteen eachother. However, the jump animtion is giving me trouble. Whenever the animation is triggered, it plays several times and then reverts to the idle after a few loops. First of all, I don’t even know hows thats possible becase it is only supposed to revert to idle once you have been grounded again, so thats pretty weird. Aslo I dont want it to loop, just run once and then hold on the end frame until the animation is complete. Is there nayway to fix this?
Here is my code: (sorry this is my entire player controller, I mean you never know what might be relevant)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Sprites;
public class playerController : MonoBehaviour
{
//MOVEMENT
[Tooltip("The maximum horizontal movement speed in units per second.")]
public float speed = 8; // movenment speed for Luca
[Tooltip("The maximum vertical jump speed in units per second.")]
public float jumpspd = 12; // jumo height for Luca
private float moveInput;
private Rigidbody2D rb;
public Transform groundCheck;
public float checkRadius;
public LayerMask whatIsGround;
public static bool canVBeset;
//ANIMATION
public Animator anim;
bool canDash = true;
public bool grounded;
bool isRunning;
public SpriteRenderer ren;
//UNUSED AS OF NOW
public static float LucaHP;
public static float LucaSP;
//TODO: MOVE ONTO NEW SCRIPT
// Use this for initialization
void Start()
{
canVBeset = true;
anim = gameObject.GetComponent<Animator>();
anim.SetBool("isRunning", false);
anim.SetBool("dashing", false);
anim.SetBool("isJumping", false);
canDash = true;
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
HandleDash();
HandleFlip();
HandleJump();
anim.SetBool("isRunning", isRunning);
if (Input.GetButton("Horizontal"))
{
isRunning = true;
}
else
{
isRunning = false;
}
if (grounded == true)
{
anim.SetBool("isJumping", false);
}
}
void FixedUpdate()
{
if (canDash == true && canVBeset == true)
{
playermove();
}
}
void playermove()
{
grounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
float axixX = Input.GetAxis("Horizontal");
//transform.Translate(new Vector2(axixX, 0) * Time.deltaTime * speed);
rb.velocity = new Vector2(axixX * speed * Time.deltaTime, rb.velocity.y);
}
void HandleJump()
{
if (Input.GetKeyDown(KeyCode.Space))
{
if (grounded == true)
{
GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jumpspd);
anim.SetBool("isJumping", true);
grounded = false;
}
}
}
void HandleDash()
{
if (Input.GetKeyDown(KeyCode.LeftShift))
{
StartCoroutine(dash());
}
}
void HandleFlip()
{
if (Input.GetKeyDown(KeyCode.A))
{
ren.flipX = true;
}
if (Input.GetKeyDown(KeyCode.D))
{
ren.flipX = false;
}
}
IEnumerator dash()
{
if (canDash == true)
{
canDash = false;
anim.SetBool("dashing", true);
if (ren.flipX == true)
{
GetComponent<Rigidbody2D>().velocity = new Vector2(-jumpspd , GetComponent<Rigidbody2D>().velocity.y);
}
else
{
GetComponent<Rigidbody2D>().velocity = new Vector2(jumpspd , GetComponent<Rigidbody2D>().velocity.y);
}
yield return new WaitForSeconds(0.2f);
anim.SetBool("dashing", false);
canDash = true;
}
}
}