x


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.

more ▼

asked Mar 23 '10 at 04:55 AM

DigitalDogfight gravatar image

DigitalDogfight
137 8 10 16

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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); 
   } 
}  
more ▼

answered Mar 24 '10 at 08:56 PM

DigitalDogfight gravatar image

DigitalDogfight
137 8 10 16

(comments are locked)
10|3000 characters needed characters left

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;
more ▼

answered Mar 23 '10 at 06:07 AM

qJake gravatar image

qJake
11.6k 43 78 161

I like what you were getting at with the parent of the spawnPoints but couldn't get it working correctly. Used an Array instead. 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!

Mar 24 '10 at 08:47 PM DigitalDogfight
(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Sep 05 '12 at 09:56 AM

olivierus gravatar image

olivierus
1 1 2

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1678
x573
x184
x9

asked: Mar 23 '10 at 04:55 AM

Seen: 8791 times

Last Updated: Sep 05 '12 at 09:56 AM