- Home /
Walls with doors in Unity3D
Hello! I've been trying to make an enemy come out of a door and then follow the player. It has to follow the player, and if the player dodges it, it has to hit a wall. I've got all the dodging and following sorted out thanks to this site, but here is the problem. I use a Plane as a wall. I want the enemy come through it, but I can't because if the enemy touches the wall, it gets destroyed. Is there a way to prevent this from happening?
Answer by bhavinbhai2707 · Jan 22, 2018 at 07:01 PM
Just set a bool in a script called shouldbeDestroyed = false; and once the Enemy passes the door and all animations and sort of stuff is done then simply make that bool true i.e, shouldbeDestroyed = true; and once the bool is true then only make the enemy destroyable
if(shouldbeDestroyed)
{
DestroyEnemy();
}
void DestroyEnemy()
{
//your code that controls and destroys enemies
}
How can I make the void DestroyEnemy()
if I have OnTriggerEnter
there?
what i wanted to say is once the enemy has actually passed the door then by using triggers just set bool ie shouldbeDestroyed as true it means now the enemy can be destroyed. now consider DestroyEnemy as a function that actually contains the code u r using for destroying them for eg:-
void DestroyEnemy()
{
if(shouldbeDestroyed)
Gameobject.Destroy(enemy);
}
//and in case of triggers u can do is
private void OnTriggerEnter(Collider thecollider)
{
if(thecollider.tag == "Enemy")
{
DestroyEnemy();
or,
if(shouldbeDestroyed)
Gameobject.Destroy(enemy);
}
}
Awesome! With a little bit of tweaking I got this to work! Thank you!
Your answer
![](https://koobas.hobune.stream/wayback/20220612151056im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do I make this switch destroy an object? 1 Answer
Removing instantiated objects?? 1 Answer
Make an enemy follow along a wall 2 Answers
Destroy parts of a Wall 2 Answers
My door causes lag spikes. 1 Answer