Animation/script problem [begginer]

Hi, i’m having a animation/scripting trouble. i did all the codes fine but there is one thing that i discovered but still can’t solve. Here goes:
when i quickly press different button arrows in a short time, the animations do not respond correctly. I guess the reason is due the command Input.Getaxis(“Vertical”) and (“Horizontal”) are float values that go between -1 to 1.
Example: i’m holding left arrow and quickly press down, the animation changes to down animation but returns to left position animation.
Here is the only reason i could imagine, The values are still going to 0, then the animation with the respective value at moment different of 0 triggers again and somehow get stucked in walking animation not returning to the last command animation or static animation.

If i am totally wrong about everything is fine, but please give me a solution.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour {

public GameObject player;
public Animator animate;

public float speed=0.025f;
public bool walkleft = false;
public bool keepsideleft = false;
public bool front = false;
public bool back = false;
public bool keepsideright = false;
public bool walkright = false;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
float h = Input.GetAxis(“Horizontal”);

float v = Input.GetAxis(“Vertical”);

if (h>0 && h<0.10)
{
Debug.Log(“Right”);
keepsideright = true;
keepsideleft = false;
walkleft = false;
walkright = false;
backfront = false;

}
if (h>= 0.10 && h<=1)
{
walkright = true;
keepsideright = false;
keepsideleft = false;
walkleft = false;
backfront = false;

player.transform.Translate(h*speed, 0, 0);

}
if (h<0 && h> -0.10)
{
Debug.Log(“Left”);
keepsideleft = true;
keepsideright = false;
walkleft = false;
walkright = false;
back = false;
front = false;

}
if (h<= -0.10 && h>=-1)
{
walkleft = true;
keepsideright = false;
keepsideleft = false;
walkright = false;
back = false;
front = false;
player.transform.Translate(h*speed, 0, 0);

}
if (v < 0)
{
Debug.Log(“front”);
front = true;
keepsideright = false;
keepside = false;
walk = false;
walkright = false;
back = false;
speed = 0;
}
if (v > 0) {
Debug.Log(“back”);
back = true;
keepsideright = false;
keepside = false;
walk = false;
walkright = false;
front = false;
speed = 0;

}

animate.SetBool(“keepsideleft”, keepsideleft);
animate.SetBool(“front”, front);
animate.SetBool(“keepsideright”, keepsideright);
animate.SetBool(“walkleft”, walkleft);
animate.SetBool(“walkright”, walkright);
animati.SetBool(“back”, back);

}
}
obs.: i changed some name for easier understanding like front and back, i’m not using diferent names in animator and script.


Will need a bit more to go on to be sure. In your scenario, would you want the animation to “stay” on down, if you just quickly pressed it (but let go?).
You said that when it returns to the other animation, that is not correct, so I’m checking.

precisely, the animation should stay down as usual, but in these circunstances, the animation returns to walking animation i don’t know how, walking animation is only able when i’m holding the right or left arrows. when i’m not pressing anything the value goes to 0 and the animation in 0 is static, facing the arrow button position of the last button pressed.

It’s still not completely clear to me, sorry! :slight_smile: I just want to be sure I understand before I give you an answer that may otherwise be wrong/incomplete.

So, say you’re walking left or right, then you press down or up. You want it to switch the animation to down or up (and move that way, maybe?), but then you release (the up/down), but you’re still holding the left/right…
Now what should happen? Do you ignore the Axis until a new GetKeyDown is pressed? Do you resume your animation?

:slight_smile:

if i press for a really short time, the animation walking returns, but if i press down/up a bit longer there’s no animation problem.

I’m sorry, but that still doesn’t really answer my question… You are more describing what is “wrong” to you, but not helping me understand what your goal is (Sorry).
Could you answer the questions from my previous post?

my goal is to stop this bug. i have 6 animations on my current project.
1- frontal
2- back
3- left side
4- left side walking
5- right side
6- right side walking

the command i’m using is input.getaxis vertical and horizontal.

i’m having trouble and want to stop. When i’m pressing left/right to walk and stop pressing, the animation walking should stop too, but its not stoping sometimes. Here is my goals.
1- The animation walking stop when i’m stop pressing
2- i want to my character do the last animation respective to my last button command.

Okay, that was better. I just have one last question from something that confused me in a prior post.
If you’re walking left and you continue holding left, and press down and release down, and you’re still holding left: do you want animation = left or animation = down?

In addition to my last question, I think maybe you want to “keep left side” to true when you’re walking left.
Then set your animator up to match - something like : keep left side == true and walk left == false to face left.
This way, when you have no ‘h’ axis, for example, it will keep your last facing direction, but not be walking.
Is that what you wanted/meant?

well i’m not so advanced yet, but the last command should work as well, in that case, left animation.

yes it is. i didn’t put a false term on animator transitions, only 1 true for each transition.

Well, if you modify your code so it’s something like this:

if(h < 0) {
   faceLeft = true;
   walkLeft = true;
 }
if(h == 0) {
  walkLeft = false;
  walkRight = false;
  // do not put faceLeft = false here. only set faceLeft false if you're facing right, down, or up*
 }
// try a small sample.

Something like that you could try?

And as for your response that the left animation should continue, that’s good to know. Now I won’t ask about that anymore :slight_smile:

1 Like

yes! i just figure out before i read this, the 0 has no fuction, i think is the reason of the bug. You confirmed that to me, thanks.

Cool hope you can get it working with that information :slight_smile: Good luck.

Now i have a new problem, the animation walking don’t stop at the time i stop pressing, the animation has 4 frames and sometimes the animation complete the 4 frames even if i’m already stopped pressing. That’s awful.

lol. Ya, there’s always a “new” problem. I think that may be “has exit time”. You could try unchecking that.
Sorry, I get confused with these things, too, sometimes. Let me know if that works, though.

Oh man, The bug returns, even with the code you give continues to walking animation.

lol please post your updated script with code tags, so we / I can see what you have now, instead of guessing :slight_smile:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour {
public GameObject player;
public Animator animate;
public float speed=0.025f;
public bool walkleft = false;
public bool keepsideleft = false;
public bool front = false;
public bool back = false;
public bool keepsideright = false;
public bool walkright = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
float h = Input.GetAxis(“Horizontal”);
float v = Input.GetAxis(“Vertical”);
if (h>0)
{
walkright = true;
keepsideright = true;
keepsideleft = false;
walkleft = false;
backfront = false;
player.transform.Translate(hspeed, 0, 0);
}
if (h<0 )
{
walkleft = true;
keepsideright = false;
keepsideleft = true;
walkright = false;
back = false;
front = false;
player.transform.Translate(h
speed, 0, 0);
}
if(h==0)
{
walkleft = false;
walkright = false;
}
if (v < 0)
{
Debug.Log(“front”);
front = true;
keepsideright = false;
keepside = false;
walk = false;
walkright = false;
back = false;
speed = 0;
}
if (v > 0) {
Debug.Log(“back”);
back = true;
keepsideright = false;
keepside = false;
walk = false;
walkright = false;
front = false;
speed = 0;
}
animate.SetBool(“keepsideleft”, keepsideleft);
animate.SetBool(“front”, front);
animate.SetBool(“keepsideright”, keepsideright);
animate.SetBool(“walkleft”, walkleft);
animate.SetBool(“walkright”, walkright);
animati.SetBool(“back”, back);

Please edit your post, after you take a moment reading this page: Using code tags properly

:slight_smile: Thank-you!!

Just adding to what methos said, you have to get rid of the exit time during transitions or it will be completely unresponsive and throw you off. Make sure every box is unchecked, especially when you are doing the coding. You might want to adjust animations that don’t blend well afterwards.