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
1
Question by nayku · Apr 18, 2020 at 09:13 AM · spawning problemsspawn points

How to choose random spawn point and spawn within its limit?

Greetings,

I'm trying to have my GameManager object (wave spawner script) spawn enemies at set locations -- and at a random location inside them.

I have a public Transform[] Spawnpoints; array in my manager in order to create more spawn points through the inspector. If I want to spawn enemies at these positions, then I can simply use this function:


 private void SpawnEnemy(Transform enemy)
     {
         Debug.Log("Spawning Enemy" + enemy.name);
         Transform SP = SpawnPoints[Random.Range(0, SpawnPoints.Length)];
         SP.position = AreaSpawn.SpawnLocation();
         Instantiate(enemy, SP.position, SP.rotation);
     }


I use an empty game object to choose my spawn locations, which I've turned into a prefab. However, I have a separate script that is attached to the spawn locations that looks like this:

 using UnityEngine;
 
 public class AreaSpawnPoint : MonoBehaviour
 {
 
     public Vector3 Center;
     public Vector3 Size;
 
     public Vector3 SpawnLocation()
     {
         Vector3 Position = Center + new Vector3(Random.Range(-Size.x / 2, Size.x / 2), Random.Range(-Size.y / 2, Size.y / 2), Random.Range(-Size.z / 2, Size.z / 2));
         return Position;
     }
 
     private void OnDrawGizmosSelected()
     {
         Gizmos.color = new Color(0, 0, 1, 0.7f);
         Gizmos.DrawCube(Center, Size);
     }
 }


In a nut shell, the public Vector3 SpawnLocation() returns a random location within the cube every time it's called. I apply this script to my spawn location objects, and create a specific size spawn area where they are allowed to spawn. (see below to get an idea) alt text

Problem My issue is I'm not sure how I can choose a random spawn location and use the random spawn location inside it. Currently, (if you look at the screenshot above) I can reference only one of the spawn location objects and it works with just that one spawn location. How can I reference all of the locations? I'm sure there's a way, but I'm not quite sure how.

Thanks

13fb4e52b2e1225fc2560988a968991c.png (370.9 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

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

Related Questions

Why doesn't my spawning code work? 2 Answers

Create GameObject On Click Not Working 0 Answers

Multi coulored Lighting and acid physics+ spawnpoints -1 Answers

wave spawn problems 1 Answer

Spawn object And assetId 0 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