Long question about animation and triggers working strange
Hi there. Im working on creating plants vs zombies clone (undemy courses)i have 2 attackers: lizard and fox. lizard has attacker, health and lizard script on it. fox got attacker, health and fox script. all animations are ready and work fine atm. i enter playmode and my fox jumps over the stone and goes further until she reaches defender. then fox begins to attack and defender is losing health. lizard works great 2. he attacks all defenders including stones. but after stone\defender is destroyed, they stuck in attck mode and isAttacking animation keeps going on both attackers. and its fine atm. for fixing that i put this piece of code in ATTAKCER script in update function(doing this according to what i see in udemy tutorial)
if (!currentTarget)
{
animator.SetBool("isAttacking", false);
}
and heres there problems begin((( only one of my attackers in scene works fine( the last placed in scene) if lizard work bad - he ignores all stones and defenders and moves through it( but all triggers work well according 2 debugger) and if fox works bad- she still jumps over the stones, but ignore defenders and path through it. Debug.Log shows me that every collision is detected . but animation isnt triggered. if i leave only 1 attacker in my scene - it works correct, but 2+ attackers still giving me only 1 working fine. maybe its a long stupid question> but i really need help)) thanks a lot!
public class Attacker : MonoBehaviour {
private Animator animator;
private GameObject currentTarget;
private float currentSpeed;
// Use this for initialization
void Start ()
{
animator = GetComponent<Animator>();
}
void Update ()
{
transform.Translate(Vector3.left * currentSpeed * Time.deltaTime);
}
void OnTriggerEnter2D()
{ }
public void WalkSpeed(float speed)
{
currentSpeed = speed;
}
public void StrikeCurrentTarget(float damage)
{
if (currentTarget)
{
Health health = currentTarget.GetComponent<Health>();
if (health)
{
health.dealDamage(damage);
}
}
}
public void Attack(GameObject obj)
{
currentTarget = obj;
}
}
Health script:
public class Health : MonoBehaviour {
public float health = 50.0f;
public void dealDamage(float damage)
{
health -= damage;
if(health <= 0)
{
DestroyObject();
}
}
public void DestroyObject()
{
Destroy(gameObject);
}
}
lizard has its own script LIzard:
public class Lizard : MonoBehaviour
{
private Attacker attacker;
private Animator animator;
void Start()
{
animator = GetComponent<Animator>();
attacker = FindObjectOfType<Attacker>();
}
void Update()
{ }
void OnTriggerEnter2D(Collider2D coll)
{
GameObject obj = coll.gameObject;
if (!obj.GetComponent<Defender>()) //ignores all but defenders
return;
}
else
{
animator.SetBool("isAttacking", true);
attacker.Attack(obj);
}
}
}
and here is the fox script
public class Fox : MonoBehaviour {
private Attacker attacker;
private Animator animator;
// Use this for initialization
void Start()
{
animator = GetComponent<Animator>();
attacker = FindObjectOfType<Attacker>();
}
void Update()
{ }
void OnTriggerEnter2D(Collider2D coll)
{
GameObject obj = coll.gameObject;
if (!obj.GetComponent<Defender>()) //ignores all but defenders
{
return;
}
if (obj.GetComponent<Stone>()) //breaking stones aswell
{
animator.SetTrigger("Jump Trigger");
}
else
{
animator.SetBool("isAttacking", true);
attacker.Attack(obj);
}
}
}