- Home /
How can I spawn an enemy "behind the nearest corner"?
Hey guys! How can I instantiate a object at the nearest corner so he won't just appear from dust?
Well, you'll have to deter$$anonymous$$e what's the closest corner first, and then instantiate the enemy at a reasonable distance. But won't it be easier to place the enemy there and disable it, then place a trigger to activate it?
Thanks for the answer and no...It's littlebit complicated so I have to deter$$anonymous$$e what is the closest corner. But I do not know how.
How is your scene built? What's a corner? $$anonymous$$ore information means higher chance to get an answer.
Answer by Peter G · Oct 03, 2011 at 06:34 PM
It's too difficult to figure out what a corner is at runtime so you need to pre-mark all your corners. That is place empty game objects at all the possible spawn points. Then you can put them in an array and move through them easily.
var spawnPoints : Transform[];
var enemy : Transform;
var player : Transform;
function Start () {
player = GameObject.FindWithTag("Player");
InvokeRepeating("SpawnEnemy" , 5 , 5);
}
function SpawnEnemy () {
var idealSpawn : Transform = spawnPoints[0];
var curDist = Vector3.Distance(idealSpawn.position , player.position);
//This is just a basic search algorithm. We test every item in the array to see if it is better suited than the one's before it.
//We assume that the first point is the best then we try and prove otherwise.
for ( var i = 1 ; i < spawnPoints.Length ; i++) {
//No need to test the first point.
if (Vector3.Distance(spawnPoints[i].position , player.position) < curDist ) {
//Are we closer than the old point?
if( Physics.LineCast(player.position, spawnPoints[i].position) ) {
//Is there something between the player and the point.
idealSpawn = spawnPoints[i];
curDist = var curDist = Vector3.Distance(idealSpawn.position , player.position);
}
else if ( Vector3.Dot( player.forward , (spawnPoints[i] - player.position).normalized ) < 0) {
//or is the point behind the player?
idealSpawn = spawnPoints[i];
curDist = var curDist = Vector3.Distance(idealSpawn.position , player.position);
}
}
}
Instantiate (enemy , idealSpawn , Quaternion.Identity);
}
This should pretty much do what you want. Let me just warn you about 2 oversights that I deliberately made:
There are quicker ways of doing distance checks than with Vector3.Distance(). Its just the easiest to read so I put it in there and you can change it if you want.
Possibly the more important one. This method breaks down if the character is closest to the "0" element in the array and he is already looking at it. It doesn't take that much effort to fix it. Just do a final check at the end to make sure that if the first element was the "ideal" spawn that it isn't visible.
Your answer
Follow this Question
Related Questions
Enemy not spawning correctly 1 Answer
Spawning different random objects at the same position? 2 Answers
How would I create a script that spawns objects more frequently as time goes on? 3 Answers
Error: Instantiated Enemies don't get hit 2 Answers
NullReferenceException: Instatiate with construtor OR in Inspector 1 Answer