- Home /
Mulitple spawn points with random seed.
I'm trying to write a Javascript that will spawn X-amount of enemies from 4 different platforms. They will need to spawn at random times rather than all at once. I'm guessing that I will need to use a Random class to divide up the enemyPrefabs, and at different times as well. The level is finished when all enemies are killed. The tutorials I've found only deal with spawning one enemy dependent on range from PlayerObject.
I'm thinking of having a Parent GameObject (SpawnPoints) with the script and each of the spawn points (spawn1-4) attached as children.
This is my initial thinking for the variables:
var enemyPrefab : GameObject; var maxEnemies : int;
var spawn1 : transform; var spawn2 : transform; var spawn3 : transform; var spawn4 : transform;
Any links to tutorials or other help is appreciated.
Answer by DigitalDogfight · Mar 24, 2010 at 08:56 PM
Here's the code I came up with that will let you set up Random Enemies, spawnPoints, maxEnemies and min/max time to wait between each respawn.
This works great but I have one hitch to figure out. Sometimes when the Enemy is instantiated, the model will randomly default to the base T-Pose with no components. Furthermore, any other instances spawned at that spawnPoint will also be locked in the base T-pose, overlapping. They have enough time to clear each other's location/spawnPoint so that's not it either.
Any ideas?
// Add this script to a Parent GameObject of the spawnPoints. // Note: enemyPrefab will have an AI script attached which will already Tag the Player object // so it won't be needed here.
var spawnPoints : Transform[]; // Array of spawn points to be used. var enemyPrefabs : GameObject[]; // Array of different Enemies that are used. var amountEnemies = 20; // Total number of enemies to spawn. var yieldTimeMin = 2; // Minimum amount of time before spawning enemies randomly. var yieldTimeMax = 5; // Don't exceed this amount of time between spawning enemies randomly.
function Start() { Spawn(); }
function Spawn() { for (i=0; i<amountEnemies; i++) // How many enemies to instantiate total. { yield WaitForSeconds(Random.Range(yieldTimeMin, yieldTimeMax)); // How long to wait before another enemy is instantiated.
var obj : GameObject = enemyPrefabs[Random.Range(0, enemyPrefabs.length)]; // Randomize the different enemies to instantiate.
var pos: Transform = spawnPoints[Random.Range(0, spawnPoints.length)]; // Randomize the spawnPoints to instantiate enemy at next.
Instantiate(obj, pos.position, pos.rotation);
} }
Answer by qJake · Mar 23, 2010 at 06:07 AM
I just added a component like this to a game I'm working on, and I'll explain what we did.
Create a "Spawner" game object, and add various spawn points as children, placing them in the world wherever you want them. Inside of the "Spawner" object, you'd want to do something like this:
// Place this code inside a loop or function
// whenever you want the object to spawn at a random location.
Transform[] spawnPoints = GetComponentsInChildren(typeof(Transform)) as Transform[];
Instantiate(myGameObjectPrefab, spawnPoints[Random.Range(0, spawnPoints.Length)], Quaternion.identity);
That gets all the transform components inside all of the children (which are your spawn points), and then instantiates a prefab at one of the spawn points randomly. Be sure to set the myGameObjectPrefab
variable as a public variable in your script, and then set its value using the Inspector in the editor...something like this:
public GameObject myGameObjectPrefab;
I like what you were getting at with the parent of the spawnPoints but couldn't get it working correctly. Used an Array ins$$anonymous$$d. I learned a lot from your post though, thank you.
I almost have it worked out with only one hitch to figure out. When the Enemy is instantiated randomly, them model will randomly default to the base T-Pose. Furthermore, any other instances spawned at that spawnPoint will also be locked in the base T-pose. They have enough time to clear each other's location/spawnPoint so that's not it either. Any ideas?
Thanks again!
Answer by olivierus · Sep 05, 2012 at 09:56 AM
oke, i tried some things. this one was the best working.
using UnityEngine;
using System.Collections;
public class PlayerMove : MonoBehaviour {
public int Speed;
public float Jumpheigt;
public bool Jump = false;
public AnimationClip walk;
public int totalScore;
private camerascript CS;
private float rote = 90;
private bool checkrote;
private Transform currentpos;
void Start()
{
CS = GameObject.Find("Camera").GetComponent<camerascript>();
}
void FixedUpdate ()
{
if(Input.GetKey(KeyCode.D))
{
rigidbody.AddForce(Vector3.back * Speed);
animation.Play();
if(Input.GetKey(KeyCode.W))
{
rigidbody.velocity = Vector3.up * Jumpheigt;
}
}
else if(Input.GetKey(KeyCode.A))
{
rigidbody.AddForce(Vector3.forward * Speed);
animation.Play();
if(Input.GetKey(KeyCode.W))
{
rigidbody.velocity = Vector3.up * Jumpheigt;
}
}
else if(Input.GetKeyDown(KeyCode.W))
{
rigidbody.velocity = Vector3.up * Jumpheigt;
animation.Play();
}
else
{
animation.Stop();
return;
}
}
void Update()
{
if(Input.GetKeyDown(KeyCode.D))
{
if(checkrote == true)
{
transform.Rotate(transform.rotation.y, rote, Time.deltaTime);
CS.setOn = true;
checkrote = false;
}
}
else if(Input.GetKeyDown(KeyCode.A))
{
if(checkrote == false)
{
transform.Rotate(transform.rotation.y, -rote, Time.deltaTime);
CS.setOn = false;
checkrote = true;
}
}
else
{
return;
}
}
public void score(int score)
{
totalScore = totalScore + score;
Debug.Log("je hebt : " + totalScore + " aan punten");
}
}
but it still won't work properly
please some advice
thanx a lot
Your answer
Follow this Question
Related Questions
Best way to instantiate lots of objects 0 Answers
For Loop isn't working properly! 1 Answer
Creating a light in game C# 1 Answer
Random.value sometimes doesn't instantiate my prefab(pic included) 1 Answer
Spawn Random Enemy 3 Answers