Delay while using GetButtonDown and Animator
Hello. I'm making my first 2d game with Unity. Actually i'm messing with animator and controls. The following script is supposed to set a trigger, then move my sprite, so it does a jump move. I'll take care of horizontal move after solving this.
My animator has an Idle state, with transitions to a Jump state and back; the condition is a Trigger named "isJumping", which i'm setting when a key is down. After the animation (the sprite moves its legs and body to jump), i want my character to effectively jump.
As result, it does effectively send itself in the air (depending on a jumpForce member variable), but the animation is fired only when it's already jumping. I tried to do a Debug.Log: it does the same thing. Anything but the motion is executed with a little bit of delay. So, how can i make it animate, then jump only after it (or after a part of it)? Thank you in advance.
Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Jump : MonoBehaviour {
public float jumpForce = 1.0f;
private Rigidbody2D rb2d;
private Animator anim;
// Use this for initialization
void Start () {
rb2d = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
void FixedUpdate () {
if (Input.GetButtonDown("Jump"))
{
Debug.Log("Jump pressed");
anim.SetTrigger("isJumping");
rb2d.AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse);
}
}
}
Are you sure Exit time
is set to 0
in the transition to your jumping state? You may also need to set the Transition Duration
to 0.
The Transition duration
helped anticipating the animation, while i had Has Exit Time
already set to false. That was the delay i noticed, so thank you. But what if i want my character to jump only after the animation is complete or at a certain point of it? Actually, animation and motion happen exactly at the same time, which is not good yet,
Your answer
Follow this Question
Related Questions
Get sprite size and position 0 Answers
How to know the animator's bones 0 Answers
How can i draw line with LineRenderer 0 Answers
How to change a button image with script? 1 Answer
Script Variable not transfering 0 Answers