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
0
Question by wasicool7 · Aug 20, 2016 at 05:47 PM · spawnx-axis

Unity2D: How to make script randomly pick a point within an object (X-axis)

Hi, is there a way of randomly spawning my objects on the y-axis. You see, I have a spawning script that randomly pick a point within an object (in this case a 3D cube) to spawn in a prefab, however my spawner only works ( randomly pick a point within an object to spawn) on the x-axis and doesn't spawn in randomly within the y-axis. This short video is a representation of what I am explaining. Thank you This is my spawning script:

    public GameObject enemy;
    public float spawnTime = 2;
  void Start() {  
      InvokeRepeating ("addEnemy", spawnTime, spawnTime);
  }

  void addEnemy() {  
      var x1 = transform.position.x - GetComponent<Renderer> ().bounds.size.x / 2;
      var x2 = transform.position.x + GetComponent<Renderer> ().bounds.size.x / 2;
      // Randomly pick a point within the spawn object
      var spawnPoint = new Vector2 (Random.Range (x1, x2), transform.position.y);
      Instantiate (enemy, spawnPoint, Quaternion.identity);
     }
   }


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

1 Reply

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

Answer by TBruce · Aug 20, 2016 at 06:21 PM

It looks like you are using the same code to spawn your enemies from the different cubes (one on top one on right).

If that is not the case then your problem is that your code is reversed. What I mean is it should look like this

 void addEnemy()
 {
     float y1 = transform.position.y - GetComponent<Renderer> ().bounds.size.y / 2;
     float y2 = transform.position.y + GetComponent<Renderer> ().bounds.size.y / 2;
     // Randomly pick a point within the spawn object
     Vector2 spawnPoint = new Vector2 (transform.position.x, Random.Range (y1, y2));
     Instantiate (enemy, spawnPoint, Quaternion.identity);
 }

If you are using the same function to add enemies from the top and the right then your code need to look more like this

 public GameObject enemy;
 public float spawnTime = 2;
 public bool fromTop = false;
 
 void Start()
 {
     InvokeRepeating ("addEnemy", spawnTime, spawnTime);
 }
 
 // this will handle instantiating objects from the top or right
 void addEnemy()
 {
     Vector2 spawnPoint = new Vector2 (transform.position.x, transform.position.y);
 
     if (fromTop)
     {
         float x1 = transform.position.x - GetComponent<Renderer> ().bounds.size.x / 2;
         float x2 = transform.position.x + GetComponent<Renderer> ().bounds.size.x / 2;
 
         // Randomly pick a x point within the spawn object
         spawnPoint.x = Random.Range (x1, x2);
     }
     else
     {
         float y1 = transform.position.y - GetComponent<Renderer> ().bounds.size.y / 2;
         float y2 = transform.position.y + GetComponent<Renderer> ().bounds.size.y / 2;
 
         // Randomly pick a y point within the spawn object
         spawnPoint.y = Random.Range (y1, y2);
     }
     Instantiate (enemy, spawnPoint, Quaternion.identity);
 }

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 wasicool7 · Aug 20, 2016 at 07:23 PM 0
Share

Thank you that did the trick. :)

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

64 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

Related Questions

Flag capture with spawning and destroying gameObject 0 Answers

[C#] Only one instantiate object is working, the rest is not.. Help meee 0 Answers

Unet: Spawn bullet - client does not have a reference of bullet 1 Answer

UNET 5.4 - Spawn an object from client...and move it around from client....? 1 Answer

Spawning limited GameObjects at a specific position not working 1 Answer


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