I’ve read many tutorials and tried different things but it’s not quite working out for me. My enemy has a rigidbody and a box collider, my bullet prefab also has a rigidbody and the circle collider is set on trigger. Not to mention my bullets go right through my enemy =/
using UnityEngine;
using System.Collections;
public class EnemyHealth : MonoBehaviour {
public float health = 10f;
public float damageAmount = 10f;
Animator anim;
// Use this for initialization
void Awake () {
anim = GetComponent <Animator> ();
}
// Update is called once per frame
void OnCollisionEnter2D (Collision2D col) {
if (col.gameObject.tag == "Bullet") {
TakeDamage (col.transform);
} else {
// Find all of the colliders on the gameobject and set them all to be triggers.
Collider2D[] cols = GetComponents<Collider2D> ();
foreach (Collider2D c in cols) {
c.isTrigger = true;
}
{
anim.SetTrigger ("EnemyDeath");
}
}
}
void TakeDamage (Transform Enemy)
{
health -= damageAmount;
}
}