Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DigitalDogfight · Mar 23, 2010 at 04:55 AM · instantiaterandomrespawnseed

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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); 

} }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image joseaguirre · Feb 23, 2014 at 07:37 AM 0
Share

unknown identifier "i"

avatar image
1

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;
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DigitalDogfight · Mar 24, 2010 at 08:47 PM 0
Share

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!

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges