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 /
  • Help Room /
avatar image
1
Question by bhuddaboom · May 10, 2016 at 06:04 AM · randomspawning

Spawning enemies using insideUnitCircle :

I'm trying to create a prefab, that I can place anywhere on a map (2D top down shooter) using insideUnitCircle to randomise the spawns somewhat but keeping them inside a general area and not outside of map boundaries. Here is my script so far:

public class SpawnCircle : MonoBehaviour {

 public GameObject enemyType;
 public int enemyCount;
 public float spawnDelay;
 public float spawnRate;
 public float spawnRadius;
 private int counter;
     
 // Use this for initialization
 void Start (){
     counter = 0;
 
     
     
 }
 void OnTriggerEnter2D(Collider2D other) {
     if (other.gameObject.tag == "Player") {
         Destroy (GetComponent<Collider2D> ());
         InvokeRepeating ("Spawn", spawnDelay, spawnRate);
     }

     
 }
 void Spawn() {
                             
     Instantiate(enemyType, transform.position = Random.insideUnitCircle * spawnRadius, transform.rotation);
     counter = counter + 1;
     if (counter == enemyCount)
         Destroy (this.gameObject);
     
 }

} Unfortunately this will place the centre of the spawn area at map co-ordinates 0,0.

How can i move the centre of the circle to where i have placed my prefab? Any help would be much 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

2 Replies

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

Answer by bhuddaboom · May 12, 2016 at 10:02 AM

Ok, after a little research i found this: http://answers.unity3d.com/questions/500369/teleport-using-randominsideunitcircle-relative-to.html and changed my code to:

     newPosition = Random.insideUnitCircle * spawnRadius;
     newPosition.x += transform.position.x;
     newPosition.y += transform.position.y;
                             
     Instantiate (enemyType, transform.position = newPosition, transform.rotation);
     newPosition.x = 0;
     newPosition.y = 0;

(I needed to reset newPosition each time so the spawnpoint won't wander all over the map)

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 NoseKills · May 10, 2016 at 10:29 PM

Add the random position to the SpawnCircle's current position

 Instantiate(enemyType, transform.position + (Random.insideUnitCircle * spawnRadius), transform.rotation);

What you previously had inside the Instantiate() call

 transform.position = Random.insideUnitCircle * spawnRadius

was setting the SpawnCircle's position to a random spot around 0,0 and spawning the item in that same place.

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 bhuddaboom · May 11, 2016 at 08:44 AM 0
Share

Hmm, this gives me this error: Assets/SpawnCircle.cs(30,51): error CS0121: The call is ambiguous between the following methods or properties: UnityEngine.Vector2.operator +(UnityEngine.Vector2, UnityEngine.Vector2)' and UnityEngine.Vector3.operator +(UnityEngine.Vector3, UnityEngine.Vector3)'

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to spawn a prefab every 5 second randomly between a set range? 0 Answers

How to make multiple objects spawn at random order and move ? 1 Answer

I need help making my objects spawn correctly. 0 Answers

Random.Onunitsphere confusing different spheres 1 Answer

Making an object spawned from a list became a child of the player? 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