// Use this for initialization void Start() { animateComp = GetComponent <Animator>(); if (animateComp == null) { Debug.LogError("Animator not found."); } curState = GetComponent <EnemyBehavior>().currentState; currentFacing = Facing.South; oldPos = transform.position; }
//update the sprite facing, and animation speed private void updateSprite(float x, float y) { if (animateComp == null) { return; } Facing newFacing = Facing.Unknown; EnemyBehavior.EnemyState newState = GetComponent <EnemyBehavior>().currentState; bool run = false; bool walk = false; bool stun = false; //set animation speed animateComp.SetFloat("Horizontal", x); animateComp.SetFloat("Vertical", y); //update state setting switch (newState) { case EnemyBehavior.EnemyState.Run: run = true; break; case EnemyBehavior.EnemyState.Stunned: stun = true; break; default: walk = true; break; } //determine strongest directional if (Mathf.Abs(x) > Mathf.Abs(y)) { if (x > 0) { newFacing = Facing.East; } else { newFacing = Facing.West; } } else if (Mathf.Abs(y) > Mathf.Abs(x)) { if (y > 0) { newFacing = Facing.North; } else { newFacing = Facing.South; } } if (newState != curState) { animateComp.SetBool("Run", run); animateComp.SetBool("Stunned", stun); animateComp.SetBool("Walk", walk); if (curState == EnemyBehavior.EnemyState.Stunned) //exit stunned { animateComp.SetTrigger("NewFacing"); } curState = newState; } if (newFacing != currentFacing) { if (newFacing == Facing.North || newFacing == Facing.South) { animateComp.SetBool("VerticalFacing", true); } else { animateComp.SetBool("VerticalFacing", false); } animateComp.SetTrigger("NewFacing"); currentFacing = newFacing; } }