Unity C# Animator Transitions

3.4k views Asked by At

I'm trying to get my enemies run animation to play when he is moving, and switch back to idle when he has stopped. However my current code doesn't seem to be doing this and instead my enemy remains in the idle state constantly. I have checked that my variables are being set but they just don't seem to be getting filtered through to my animator to make the transitions. I also have an error which doesn't seem to stop the game from playing but pops up in the console. The error is Controller 'Pirate': Transition " in state 'Idle_Pirate' uses parameter 'walking' which is not compatible with condition type. I assume this is the culprit but after trying a few different suggestions from googling I am struggling to find a solution. This is the code from the script attached to my enemy. Apologies if it is a little crude I am still learning. Any help is greatly appreciated.

using UnityEngine;
using System.Collections;

public class AI : MonoBehaviour {

    public float walkSpeed = 2.0f;
    public float wallLeft = 0.0f;
    public float wallRight = 2.0f;

    float walkingDirection = 1.0f;
    Vector3 walkAmount;
    float timeCheck = 0.0f;
    float walkCheck = 0.0f;

    public float maxSpeed = 5f;
    bool facingRight = true;
    bool idle = true;

    Animator anim;

    // Use this for initialization
    void Start () {
        anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    void FixedUpdate () {



    }

    void Update () {
        if (timeCheck >= 2.0f) {
            walkAmount.x = walkingDirection * walkSpeed * Time.deltaTime;
            if (walkingDirection > 0.0f && transform.position.x >= wallRight) {
                walkingDirection = -1.0f;
                Flip ();
            } else if (walkingDirection < 0.0f && transform.position.x <= wallLeft) {
                walkingDirection = 1.0f;
                Flip ();
            }
            walkCheck = walkCheck + Time.deltaTime;
            idle = false;
        }

        if (walkCheck >= 2.0f) {
            idle = true;
            walkAmount.x = 0;
            timeCheck = 0.0f;
            walkCheck = 0.0f;
        }

        timeCheck = timeCheck + Time.deltaTime;
        transform.Translate(walkAmount);
        anim.SetBool ("walking", idle);

    }

    void Flip () {

        facingRight = !facingRight;
        Vector3 theScale = transform.localScale;
        theScale.x *= -1;
        transform.localScale = theScale;

    }
}
1

There are 1 answers

0
CMA On

Managed to figure it out myself anyway, turns out I was using my animation in my animator instead of my sprites, must of dragged the wrong thing at some point. Thanks to those who took the time to read anyway.