Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 MillZzie · Apr 02, 2014 at 02:06 PM · prefabs

Help on specific spawn position for a single prefab

Hi I am having real trouble trying to figure this out at the moment I have a script which allows me to be able to insert as many enemy prefabs in to the inspector and will allow me to spawn them within a local boundary, my problem is I have to brefabs one which is a butterfly and a bird, the butterfly is fine and that needs to be able to spawn anywhere in the local boundary but I need a way to be able to spawn the bird randomly but no lower than 5 in the y-axis, can anyone help, be much appreciated as im racking my brian round it, I know its probably some think simple that im over looking but any help would be great, THANKS.

 using UnityEngine;
 using System.Collections;
 
 public class EnemySpawnerController : MonoBehaviour {
 
     private Collider collider;
 
     int hazardCount;
     public int hazardCountMax;
     public int hazardCountMin;
 
     int WaveWait;
     public int WaveWaitMin;
     public int WaveWaitMax;
 
     float WaitMax =10f;
     float WaitMin = 5f;
 
     public float spawnWait;
 
     public float SpawnValueX;
     
     public GameObject[] EnemyPrefabs;
     
     void Start () 
     {
         collider = GetComponent<Collider> () as Collider;
         StartCoroutine (SpawnWaves ());
     }
     
     // Update is called once per frame
     void Update () 
     {
     
     }
 
     private Vector3 MakeRandomSpawnPosition()
     {
         Vector3 local = collider.transform.position;
         local.x = SpawnValueX - (collider.transform.localScale.x/2);
         local.y = Random.Range(local.y, local.y + collider.transform.localScale.y) - (collider.transform.localScale.y /2);
         local.z = 0f;//Random.Range(local.z, local.z + collider.transform.localScale.z) - (collider.transform.localScale.z /2);
         
         return local;
     }
 
 
     IEnumerator SpawnWaves ()
     {
         float currentWait = Random.Range (WaitMin, WaitMax);
         WaveWait = Random.Range (WaveWaitMin, WaveWaitMax);
         hazardCount = Random.Range (hazardCountMin, hazardCountMax);
 
         yield return new WaitForSeconds (currentWait);
         while (true)
         {
             for (int i = 0; i < hazardCount; i++)
             {
                 GameObject gameObjectInstance = MakeRandomGameObject(MakeRandomSpawnPosition());
                 //Instantiate (enemyPrefab, spawnPosition, spawnRotation);
                 yield return new WaitForSeconds (spawnWait);
             }
             yield return new WaitForSeconds (WaveWait);
         }
     }
 
     private GameObject MakeRandomGameObject(Vector3 position)
     {
          
         GameObject enemyPrefab = EnemyPrefabs[Random.Range(0, EnemyPrefabs.Length)];
 
         Vector3 spawnPosition = MakeRandomSpawnPosition();    
         Quaternion spawnRotation = Quaternion.identity;
         GameObject gameObjectInstance = Instantiate(enemyPrefab, spawnPosition, Quaternion.identity) as GameObject;
         
         return gameObjectInstance;
     }
 
     void OnTriggerExit (Collider other)
     {
         if (other.gameObject.tag == "Enemy") 
         {
             Destroy (other.gameObject);
         }
         else if(other.gameObject.tag == "PickUp")
         {
             Destroy (other.gameObject);
         }
     }
 }
 
Comment
Add comment · Show 2
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 koray1396 · Apr 02, 2014 at 03:05 PM 0
Share

you already have $$anonymous$$akeRandomSpawnPosition method, you need to change it as y position will be greater than 5.

if local.y < 5, random range would be starting from 5?

avatar image Infinite_Gamer · Apr 03, 2014 at 02:50 AM 0
Share

or you could just make an empty game object and make the player spawn from there

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by sedativechunk · Apr 03, 2014 at 03:08 AM

What you are looking for is a randomizer. Randomizers can help bring your game to life and help create unpredictablility which replicates that of A.I. decisions.

 // Create a random position for the game object
 // For now I'll just use the transform.position but you could create a new vector 3
 Vector3 position = transform.position;
 
 // Generate a random range for the position of your object on the X and Y axis (you could also use the Z axis), this  could will generate a random number between 1 and 5. If you want different ranges/bounds you can modify them and even include negative numbers
 float x = Random.Range(0, 5f);
 float y = Random.Range(0, 5f);
 
 // Add the random values to the position
 position.x = x;
 position.y = y;
 
 // Update the transform position
 // It's best to use a new variable for C# because you cannot directly modify the transform position
 transform.position = position;

Just be careful with randomizers. They can get slightly resource demanding. I would only use it in the "Start()" call when instantiating an object. But doing that should allow your objects to spawn randomly.

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
avatar image
0

Answer by wijesijp · Apr 03, 2014 at 05:29 AM

have a look at Mathf.Clamp function also

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

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Link GUIText to a prefab? 2 Answers

Multiple Doors and Buttons 0 Answers

Accessing a certain prefab and it's children 1 Answer

Why have some of my prefabs and animations files gone blank?? 1 Answer

Spawning different items from different prefabs 2 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