- Home /
Spawn Point Problems
Hi guys so I am new to coding and need some help. This is the first code I've attempted to write on my own, and I cant seem to get it to work. Basically the idea is that when the player enters a certain box, it waits a period of time then spawns one monster. The problem is that A: It seems to spawn random numbers of monsters in the time it takes to process the code that tells it to stop, so one time it will spawn 50-100 another it will spawn 3 etc. B: Anything will activate the trigger that starts the spawning. I can activate it by shooting it with my gun, or the enemies that spawn will activate it and create an infinite loop. I tried to follow the exact coding that someone else online wrote to specify only the player could activate it but with no effect. The wait time function works between spawns, luckily.
var fight : boolean;
var minWait : int;
var maxWait : int;
var waitTime : int;
var spawn : boolean;
var enemiesSpawned : int;
var enemy : GameObject;
var player : GameObject;
var breach : boolean;
function Start()
{
fight = false;
spawn = false;
breach = true;
minWait = 5;
maxWait = 20;
waitTime = Random.Range(minWait, maxWait);
player = GameObject.Find("Player");
}
function Update()
{
if(fight == true && breach == true)
{
Encounter();
}
}
function Encounter()
{
spawn = true;
if(spawn == true)
{
Invoke("Spawn", waitTime);
}
}
function Spawn()
{
if(spawn)
Instantiate(enemy, transform.position, transform.rotation);
enemiesSpawned +=1;
NewWaitTime();
spawn = false;
breach = false;
}
function NewWaitTime()
{
waitTime = Random.Range(minWait, maxWait);
yield WaitForSeconds(waitTime);
Reset();
}
function Reset()
{
breach = true;
}
function OnTriggerEnter(other : Collider)
{
if(other.gameObject.tag == "Player")
{
fight = true;
}
}
function OnTriggerExit(other : Collider)
{
if(other.gameObject.tag == "Player")
{
fight = false;
}
}
You currently have no curly braces for the
if
statement inSpawn()
. This means that the condition only applies to the following line, any subsequent lines to be executed regardless.You find the "Player" object in
Start()
, if the find operation succeeds (you can confirm by watching the inspector as it runs) then you can use "`if(other.gameObject == player)`" in theOnTriggerEnter()
andOnTriggerExit()
methods.
Apart from those points I'm finding it hard to read the purpose behind the logic you have built. I'm left with many questions: What is breach?; Why yield before calling Reset()?; Should enemiesSpawned be checked to deter$$anonymous$$e when to stop spawning?....
I would suggest mentally stepping though your ideal logic flow, and then when you have that solidified check the code to confirm it matches the ideal.
function Spawn()
{
if(spawn)
{
Instantiate(enemy, transform.position, transform.rotation);
enemiesSpawned +=1;
NewWaitTime();
spawn = false;
breach = false;
}
}
Like that? Everything else works except the random quantities of enemies now.
I mostly set up breach as an additional step to prevent the massive enemy spawns, and i chose to yield before reset because the idea was that as long as breach wasnt true the trigger wouldnt happen that way i wouldnt have to spend forever trying to figure out how to contradict the OnTriggerEnter function. also I dont really know how to code the check for deter$$anonymous$$ing how many to spawn. XD
in the trigger i wrote via a tutorial, Instantiate only spawned one, and didn't need a check so I figured that was just how that command worked XD
Answer by KellyThomas · Dec 29, 2013 at 06:48 AM
Indenting will help both yourself and others read your code.
One way to put a hard limit on the number spawned would be like this, just define maxEnemies as you see fit:
function Spawn()
{
if(spawn && enemiesSpawned < maxEnemies)
{
Instantiate(enemy, transform.position, transform.rotation);
enemiesSpawned +=1;
NewWaitTime();
spawn = false;
breach = false;
}
}
If you wanted each wave to start fresh you would then set enemiesSpawned
back to zero in OnTriggerEnter()
.
Also note:
function Update()
{
if(fight == true && breach == true)
{
Encounter();
}
}
function Encounter()
{
spawn = true;
if(spawn == true)
{
Invoke("Spawn", waitTime);
}
}
Could all be simplified to:
function Update()
{
if(fight == true && breach == true)
{
spawn = true;
Invoke("Spawn", waitTime);
}
}
thank you :) it should work fine now, once I can get it to start registering new integers, which isnt happening for some reason.
got it working :) i think it just took a while to update the script :)
Your answer
Follow this Question
Related Questions
Spawn OnTriggerEnter 0 Answers
Spawning objects on trigger does not work as expected 2 Answers
Issue With Spawning Enemies (javascript) 2 Answers
Instantiated (clone of) prefab, doesn't trigger another object's collider when inside it. 2 Answers
Want a script that can instantiate an object when MOVING inside the trigger 1 Answer