How to stop enemies from spawning through walls?
In my script, enemies spawn in a circle around the player. The problem is, they tend to spawn into walls. What I tried to do was to detect if they collide with a wall at spawn, and if they do, move them to a spawn point I set up. My Script that detects the collision looks like this:
void OnCollisionEnter(Collider collider)
{
if (collider.gameObject.tag == "wall" && alreadySpawned == false) {
alreadySpawned = true;
enemy.MovePosition(spawnPoints[Random.Range(0, spawnPoints.Length)].transform.position);
Debug.Log("I spawned");
}
}
I have no idea why it doesn't work. Maybe because I use mesh colliders? (I create simple shapes in blender fot the collider, import it to unity and use a mesh collider on it) Thanks
Answer by Arthur944 · Sep 15, 2016 at 03:28 PM
I solved it by checking in the if statement wheter the distance from the navmesh was smaller than 0.1. Still don't know why it didn't work with the colliders though.
Your answer
Follow this Question
Related Questions
Checking collision before instantiating prefab 0 Answers
Keep the players position constant between two different levels 1 Answer
As an object spawner to aprecionar a button ui ? postscript I speak Castilian 0 Answers
Check to see if the new spawn location is different than the previous one? 1 Answer