Okay Im new to unity. Ive made a script where I could jump over and kill an enemy. if I jump over it will take damage equal to 10 which is the maximum health of my enemies. than it will destroy itself after spawning ammo and effects. But every time I jump over a random enemy the last enemy that I had put from the preferb is getting killed.than I cant kill no one by jumping over but can kill by shooting. If I try to jump than i get stuck on top of them. need help.
here is the code I used in player to kill by jumping
void OnCollisionEnter2D(Collision2D collision) {
aipatrol enemy = collision.collider.GetComponent<aipatrol>();
if (enemy != null){
foreach (ContactPoint2D point in collision.contacts)
{
//for the enemy to die
if(!healthscipt) continue ;
Debug.Log(point.normal);
Debug.DrawLine(point.point,point.point + point.normal,Color.red,10);
if (point.normal.y >= 0.9f){
healthscipt.Takedamage(10);
}
//for the player to get hurt
else{
hurt();}
}
}
}
//healthscipt is the script to reference enemy health. heres the script I used for enemy death
public void Takedamage(float damage){
hp -= damage;
if(hp <= 0f ){
Die();
}
}
void Die (){
if(diepeffect != null){
Instantiate (diepeffect,transform.position,Quaternion.identity);}
float spawnrate = Random.Range(0,100f);
if (spawnrate <= chancetospawnammo){
Instantiate(ammodrop,transform.position,Quaternion.identity);
}
Destroy(gameObject);
}
so I need help.