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 =;
	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,
			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);
		else {
			animator.SetFloat("arrowKey", 0.0);
		if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A)) {
			transform.Rotate(0, -1, 0);}
		if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D)){
			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

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.