- Home /
Transitions being ignored in Animator Tree
So like I been having this problem for awhile and I'm honestly not sure if it's the code or the Animator that I have set up wrong. So my problem is that I have sort of two idle animations. One is the main default animation that it starts with and then I wanted to have when you target an enemy, it switches to an attack mode idle mode. Probably is that even with or without an exit time, the animations overlap each other and it still tries and goes back to the main idle even though the transition set up shouldn't allow it.
I'm really scratching my head cause if I can't get this to work, I won't be able to set up other animations correctly either.
Code is Below (please ignore the fragments of legacy code, it is a WIP upgrading code lol):
#pragma strict
var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;
private var moveDirection : Vector3 = Vector3.zero;
var animator : Animator;
var target: Transform;
function Update() {
var controller : CharacterController = GetComponent(CharacterController);
animator = GetComponent(Animator);
if (controller.isGrounded) {
// We are grounded, so recalculate
// move direction directly from axes
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
// Directional Movement with Arrow Keys and WASD
if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.W) ||
Input.GetKey(KeyCode.S)) {
animator.SetFloat("arrowKey", 1.0);
animator.Play("note_walk01");
}
else {
animator.SetFloat("arrowKey", 0.0);
animator.Play("note_idel01");
}
if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A)) {
//animator.Play("note_idel01");
transform.Rotate(0, -1, 0);}
if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D)){
//animator.Play("note_idel01");
transform.Rotate(0, 1, 0);}
// Attack animations to be triggered when attacking
if (Input.GetKeyDown(KeyCode.Tab)){
target = GameObject.FindWithTag("Enemy").transform;
animator.SetBool("foundEnemy", true);
}
if (Input.GetKey(KeyCode.F)) {
animator.SetFloat("attackButton", 1.0);
}
else {
animator.SetFloat("attackButton", 0.0);
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
controller.Move(moveDirection * Time.deltaTime);
}
And my animation tree that I have currrently up and running.... kinda
Answer by DiegoSLTS · May 08, 2015 at 07:04 PM
It's hard to know where's the problem without the animator and the transitions, but I see odd things in your code.
You're using float parameters for things that look like booleans. It's not a big deal, but it can be confusing.
You set those float values to 1 or 0 on every frame instead of using only the frames when the keys are pressed or released. Again, is not a big deal, but it might confuse you later.
About those 2, I'd use bool parameters instead of floats, and write things this way (just an example):
if (Input.GetKeyDown(KeyCode.F)) {
animator.SetBool("attackButton", true);
}
else if (Input.GetKeyUp(KeyCode.F)){
animator.SetBool("attackButton", false);
}
What looks more important is that in some places you're setting parameters (which might trigger some transition to a new animation clip) and immediatelly use the "Play" method. It's like you're mixing stuff, you're moving between states using an indirect approach (setting values and letting the states change if needed) and manually. I don't know how to fix that, you should properly set your transitions to work with only one scheme, if pressing "Up" is supposed to trigger some animation, then "SetFloat("arrowKey",1)" should be enough to trigger it, without the Play line.
:D so I got the idle issue fixed! There is just a problem now with the animations not looping but I figure that the animation itself that is the issue right?
EDIT: Yes it was the animation and thank you so much for helping me find the issue!!!
Your answer
![](https://koobas.hobune.stream/wayback/20220613194802im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Animation will not Return to Proper State 2 Answers
Mecanim transitions sometimes don't work 4 Answers
2D Animation Instant Transition 2 Answers
Animating Transitions for GUI Results 0 Answers
Have one animation segue into another 2 Answers