4
I just created animation to run and jump with Unity, but I have a problem when the player goes back to the initial state of Idleplayer, when I stop running or jump it continues for another second, I have tried to decrease the frames but nothing. Follow my code below and a screenshot of the animation setup:
public class Player : MonoBehaviour
{
public float velociadade;
public Transform player ;
public Transform Ground;
public Animator animator;
public bool isGround;
public float force = 200f;
public float jumpTime = 0.5f;
public float jumpDelay = 0.5f;
public bool jumped ;
void Start ()
{
animator = player.GetComponent<Animator> ();
}
void Update ()
{
Movimentar ();
}
void Movimentar ()
{
isGround = Physics2D.Linecast(this.transform.position,Ground.position,1<<LayerMask.NameToLayer("Plataforma"));
animator.SetFloat ("run", Mathf.Abs (Input.GetAxis ("Horizontal")));
if (Input.GetAxisRaw ("Horizontal") > 0) {
transform.Translate (Vector2.right * velociadade * Time.deltaTime);
transform.eulerAngles = new Vector2 (0, 0);
}
if (Input.GetAxisRaw ("Horizontal") < 0) {
transform.Translate (Vector2.right * velociadade * Time.deltaTime);
transform.eulerAngles = new Vector2 (0, 180);
}
bool up = Input.GetKeyUp(KeyCode.Space);
if (up && isGround && !jumped)
{
GetComponent<Rigidbody2D>().AddForce(transform.up * force);
jumpTime = jumpDelay;
animator.SetTrigger("jump");
jumped = true;
}
jumpTime -= Time.deltaTime;
if (jumpTime <= 0 && isGround && jumped) {
animator.SetTrigger("ground");
jumped = false;
}
}
Hello. From what I can tell, your transition rule of
runPlayer
foridlePlayer
is when the value ofrun
is less than0.1
. A possible problem I see is that the value ofInput.GetAxis()
is in the range [-1.. 1], and thus will be negative (and consequently less than 0.1! ) if the player moves to the left, triggering this transition erroneously. You may have a similar error in the other transition (fromidlePlayer
forrunPlayer
) which leads erroneously torunPlayer
?– Luiz Vieira
opa exactly, it is giving the idleplayer error to the runPlayer... I tried to change the values there and it didn’t work... so I understood the b.o is time to get the value in Input.Getaxis() correct?
– thiago.adriano26
Not exactly. The way it is, it won’t work when the character moves to the left, because the value of
Input.GetAxis()
will be negative (-1), then less than 0.1 and the animation will change fromrunPlayer
foridlePlayer
. I assumed that you also have the reverse problem, because by the way you did not understand like theInput.GetAxis()
works. Change the transition value from "less than 0.1" to "equal to 0" and see if it works. The idea is that when your character stops moving because the user stopped pressing the arrows on the keyboard, the value returned byGetAxis
will be 0.– Luiz Vieira
He’s walking both ways, the problem is when I stop pressing the arrows both sideways and up he continues for another second or so running or jumping...
– thiago.adriano26
Um... I think you’re the one who didn’t understand what I told you. Reread my comments and reread the documentation of the function
Input.GetAxis()
. The problem does not seem to be the movement, but the machine of animation states. In other words, it seems to me that your transitional rules are wrong.– Luiz Vieira
After reading the documentation a few times I understood... vlw is now working ...
– thiago.adriano26
Not at all. Since you solved it, why don’t you create an answer yourself explaining in detail what you did to fix it? I could do it, but I don’t know exactly what you’ve changed, so you can do it without a problem. Having an answer accepted is good because "mark" the question as answered and also helps more easily other people in the future who have similar problems.
– Luiz Vieira
Perfect opa, I will go up here ... dai is already recorded as solved.. vlw the tip...
– thiago.adriano26
What has been changed to fix this problem, I am in the same doubt. I wait, thank you!
– user32834