- Home /
Unity 2d count collisions ontriggerenter?
Hi.
I'm trying to write a script for my enemy. When shooting the enemy this script has to count when the bullet enters and leaves the box collider. I want to add a maximum of two shots that just fired at the enemy should make the death animation and then after 4 seconds is destroyed. Now it happens that firing only one shot the enemy is destroyed after 4 seconds, but does not do the animation death.
Some help?
script:
using UnityEngine;
using System.Collections;
public class EnemyDead: MonoBehaviour {
public float count = 2;
public Animator Animator;
void Start () {
Animator = GetComponent<Animator> ();
}
void OnTriggerEnter2D(Collider2D col)
{
if(col.gameObject.tag == "HitEnemy");
}
void OnTriggerExit2D(Collider2D col)
{
count = -1;
if(count > 0)
Animator.SetInteger ("AnimState", 3);
Destroy(gameObject, 4);
}
}
Answer by barbe63 · May 17, 2015 at 10:59 AM
Many mistakes
count = -1; -> should be count -=1; or count --; or count = count-1; and you have to put brackets with your if because right now you destroy the object even if the condition is not fullfilled.
Like this:
if(count == 0)
{
Animator.SetInteger ("AnimState", 3);
Destroy(gameObject, 4);
}
This part of your code:
void OnTriggerEnter2D(Collider2D col)
{
if(col.gameObject.tag == "HitEnemy");
}
is doing nothing. Replace all your code inside OnTriggerExit and put it in those brackets would be better like this: (remove the OnTriggerExit())
void OnTriggerEnter2D(Collider2D col)
{
if(col.gameObject.tag == "HitEnemy")
{
count--;
if(count == 0)
{
Animator.SetInteger ("AnimState", 3);
Destroy(gameObject, 4);
}
}
You should also remove all the unnecessary spaces, somes are nice to separate blocks but too much is bad.
Edit: your condition is also bad. Replace it with if (count == 0), checking if it's >0 would'nt make any sense.
Too much mistakes in a small part of code... Review all your lessons ;)
Hello Baber63 I'm glad to hear you again! :-)
Then I tried your code but it seems to work. When firing a shot, the death animation of the enemy does not work, and after 4 seconds to destroy the enemy. I do not know what the problem is, but it should work like this: After two shots that hit the enemy you should activate the animation death, the enemy falls to the ground and then destroyed after 4 seconds.
using UnityEngine;
using System.Collections;
public class Enemy$$anonymous$$uore: $$anonymous$$onoBehaviour {
public int count = 4;
public Animator Animator;
void Start () {
Animator = GetComponent<Animator> ();
}
void OnTriggerEnter2D(Collider2D col)
{
if(col.gameObject.tag == "HitEnemy")
{
count--;
if(count > 0)
{
Animator.SetInteger ("AnimState", 3);
Destroy(gameObject, 4);
}
}
}
}
I also replaced:
public float count = 2;
With
public int count = 2;
But it does not change anything because one hit to kill the enemy.
I edited my post, the last mistake is "if(count > 0)" should be if(count==0)
Also try to not name variables as reserved space names like Animator. You should rename it animator (with lower case a)
Now it works!
Except the animation after two rounds no soul. I put animator with a tiny.
modified script:
using UnityEngine;
using System.Collections;
public class Enemy$$anonymous$$uore: $$anonymous$$onoBehaviour {
public int count = 1;
public Animator animator;
void Start () {
animator = GetComponent<Animator> ();
}
void OnTriggerEnter2D(Collider2D col)
{
if(col.gameObject.tag == "HitEnemy")
{
count -=1;
if(count==0)
{
animator.SetInteger ("AnimState", 3);
Destroy(gameObject, 4);
}
}
}
}
Your answer
Follow this Question
Related Questions
Enemy trigger for player 1 Answer
How an Enemy could activate a Sound? 3 Answers
MAke an enemy attack and look at other enemies HELP? 1 Answer
Door open after destroying all the enemies. 1 Answer
Why is my character taking damage when attacking & why is it attacking multiple times per hit? 2 Answers