- Home /
Mob Spawner, choosing a random mob
I have a mob spawner script that has a timer int for how often an object spawns, variable for the prefab that I want to get spanwed and ints for how far and close the player must be for the script to disable.
But I would want to have multiple variable spots for diffrent objects and also having another variable for what % chance it has to be spawned. Any help for where to start from?
My current mob spawner scrpt:
var enemy: GameObject;
var spawnTime:float = 3f;
var player : Transform;
var CloseDistance = 3; //Close Enough
var CloseEnough : boolean = false;
var FarDistance = 12; //Far Enough
var FarEnough : boolean = false;
var isInInvoke : boolean = true;
private var thisTransform : Transform;
function Start(){
thisTransform = transform;
player = GameObject.FindWithTag("Player").transform;
}
InvokeRepeating ("Spawn", this.spawnTime, this.spawnTime);
function Spawn (){
Instantiate (enemy, thisTransform.position, thisTransform.rotation);
}
function Update(){
//Close Enough
if(Vector2.Distance(transform.position, player.position) < CloseDistance){
CloseEnough = true;
} else {
CloseEnough = false;
}
if (CloseEnough == true) {
CancelInvoke("Spawn");
isInInvoke=false;
}
if (CloseEnough == false && isInInvoke==false) {
InvokeRepeating ("Spawn", this.spawnTime, this.spawnTime);
isInInvoke=true;
}
//Far Enough and Close Enough
if(Vector2.Distance(transform.position, player.position) > FarDistance){
FarEnough = true;
} else {
FarEnough = false;
}
if (FarEnough == true) {
CancelInvoke("Spawn");
isInInvoke=false;
}
if (CloseEnough == false && isInInvoke==false) {
InvokeRepeating ("Spawn", this.spawnTime, this.spawnTime);
isInInvoke=true;
}
}
Why not just put this script on different spawner objects in different locations?
Have you tried to run this script? From what I can see, there'll be quite a few compiler errors that may crop up.
What a terrible piece of code. I like how you cache the transform in Start. Then proceed to ignore your cached variable and use the transform variable directly.
I suggest rewriting the entire script with simplicity and DRY principles in $$anonymous$$d. Run it in Unity to make sure it compiles and has the desired effect. Then consider adding more functionality.
At the moment it will be more work then its worth to keep adding to this script.
it works! Did you not understand what I wanted? I wanted a rare chance of it spawning a diffrent type of mob
Answer by alexander11 · Oct 13, 2014 at 11:53 PM
try using random.Range
Did you not understand what I wanted? I wanted a rare chance of it spawning a diffrent type of mob
Answer by MrSoad · Oct 13, 2014 at 10:58 PM
Here is some code I wrote to randomly spawn either a homing mine or normal mine, maybe this will help?
//Spawn a Mine Or Mine_Homing.
if (Random.Range(0, 10) > 6) {
if (iActive_Mines_Homing < iMax_No_Of_Mines_Homing) {
subSpawn_Mine_Homing();
} else {
subSpawn_Mine();
}
} else {
if (iActive_Mines < iMax_No_Of_Mines) {
subSpawn_Mine();
} else {
subSpawn_Mine_Homing();
}
}
Just another note, with invoke methods you can use :
if (isInvoking("Name of your invoke") {
To see if it is running rather than having to use Boolean flags everywhere.
seems like this will help, thanks! Il try to code that stuff in once I get monoDevolp working.. right now it shows errors when trying to open a script
Your answer
![](https://koobas.hobune.stream/wayback/20220613165023im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Making a List for Inventory System, Got an error in finding constructors and variables 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Changing a variable on a different object 2 Answers
Javascript int to float 1 Answer
Script is setting variables but they're being reset 1 Answer