void OnTriggerEnter(Collider other) { if (other.CompareTag(target)) { other.GetComponent <Health> ().ChangeHealth(damage); Instantiate(hitEffect, transform.position, Quaternion.identity); Vector3 dir = other.transform.position - owner.transform.position; dir.y = 0; dir.Normalize(); EnemyAI _enemyScript = other.GetComponent <EnemyAI> (); if (_enemyScript != null) { _enemyScript.AddImpact(dir, knockbackForce); } } }
void OnTriggerEnter(Collider other) { if (other.CompareTag(target)) { other.GetComponent <Health> ().ChangeHealth(damage); Instantiate(hitEffect, transform.position, Quaternion.identity); Vector3 dir = other.transform.position - owner.transform.position; dir.y = 0; dir.Normalize(); //como este script será usado tanto por el player como por el enemigo //es posible que _enemyScript sea nulo EnemyAI _enemyScript = other.GetComponent <EnemyAI> (); //debido a esto hacemos este chequeo para evitar errores if (_enemyScript != null) { _enemyScript.AddImpact(dir, knockbackForce); } } }