- Home /
Having some animation trouble
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;
}
}
}
in Update you are saying if (grounded == true) anim.SetBool ("isJumping", false)
and also saying in HandleJump that if (grounded == true) anim.SetBool("isJumping", true) and in your Update you are calling HandleJump everyframe?
I don't know if that is the problem but it has me confused. I mean after the jump you are setting grounded = false, so in the update function isJumping doesn't become false because grounded is false?
Yeah your right, I never noticed how those were kind of contradicting each other. I have an idea using a courotine that might fix it but do you have a different idea? Thanks regardless for pointing that out.
no better idea here, that is what I was thinking as well. a coroutine to set isJumping to false again. or....since the animation is already triggered on $$anonymous$$eyDown...maybe try keyUp to set grounded to true or isJumping false?
Yeah okay that did not work.... Ill put this on reddit for more guidance.
Answer by Rugbug_Redfern · Apr 15, 2018 at 11:43 PM
Go into the Project tab in unity and click on the animation file. Uncheck "Loop Mode." This will stop the animation from looping. I don't think the problem is with your code, try checking the Animator. Make sure "exit time" is set to false (unchecked) on the transitions.
Yeah so looping is unchecked and none of the transitions have exit time. So that changed it, but didnt fix it. Now it loops once, but after its done it imediately reverts back to idle, which it shouldnt becase of the booleans I have set up, this is why I think its my code.
Answer by shadowpuppet · Apr 16, 2018 at 02:19 PM
Here's an idea. Instead of having HandleJump(); in the Update function put the keydown there. I don't use bools i use SetTrigger and this works for me because as the code runs from top down and sets the trigger to start the animation, resetting the trigger doesn't affect the running of the animation once started. Maybe that is true for setting bools to true as well? so once the conditions are met to start the animation I revert the conditions so they don't repeat the animation. But not sure if setting the two conditions to false will mess up the Vector 2 code - like if it will stop running once the conditions reversed. So...maybe back to using a coroutine to do that
void Update(){
if (Input.GetKeyDown(KeyCode.Space))
grounded = false;
anim.SetBool("isJumping", true);
HandleJump();
}
}
void HandleJump()
{
if (grounded == false)
{
GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jumpspd);
grounded = true;
anim.SetBool("isJumping", false);
}
}
}
Your answer
Follow this Question
Related Questions
How can I make my guy stop running? 1 Answer
Continuously Play Animation Forwards and Backwards with Mechanim 0 Answers
Animator won't let me add transitions 1 Answer
Can't move player in Bomberman after applying simple animation 0 Answers
How can I make a state pass after a reversed state? 0 Answers