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 Dimitris4 · Apr 25, 2019 at 07:45 PM · randomspawnrandom.range

random place generator

public class enemySpawner : MonoBehaviour

{ public GameObject enemyPrefab;

public float spawnTime;

 Vector2 screenBounds;

 void Start()
 {
     screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
     StartCoroutine(alienWave());
 }

 void enemySpawn()
 {
     GameObject a = Instantiate(enemyPrefab) as GameObject;
     a.transform.position = new Vector2(screenBounds.y * 2, Random.Range(-screenBounds.x, screenBounds.x));
 }

 IEnumerator alienWave()
 {
     while (true)
     {
         yield return new WaitForSeconds(spawnTime);
         enemySpawn();
     }
 }

}

I keep spawning aliens only on this side. any solutions?alt text

στιγμιοτυπο-οθονης-10.png (76.0 kB)
Comment
Add comment · Show 1
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 DCordoba · Apr 25, 2019 at 11:59 PM 0
Share

the problem is there

 a.transform.position = new Vector2(screenBounds.y * 2, Random.Range(-screenBounds.x, screenBounds.x));

if you see on your global space, problably x axis is the horizontal axis , try some like

 a.transform.position = new Vector2((Random.Range (0,1))*screenBounds.y * 2, Random.Range(-screenBounds.x, screenBounds.x));

(Random.Range (0,1)returns a int either 0 or 1, not a float value betweem them so, the aliens will spawn on the left y = 0 or right y = screenBounds.y*2

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Dimitris4 · Apr 26, 2019 at 12:03 PM

@DCordoba its not the same except instead of spawning on the sides it spawns in the middle. Any other idea?

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 DCordoba · Apr 26, 2019 at 05:51 PM 0
Share

I forgot to Random.Range($$anonymous$$, max) is with $$anonymous$$ inclusive but max exclusive so, x axis always will be 0...

ammm, sorry, my bad xd

try, a.transform.position = new Vector2((Random.Range (0,2))*screenBounds.y * 2, Random.Range(-screenBounds.x, screenBounds.x));

this will do random between 0 and 1

also please provide info about camera location, I believed, purely empiric to (0,0) was on the left-bottom part of the scene... this doesnt seem to be the case, so...

lets make it jump between two fixed values, (-1 and 1) if your camera is centered on 0 this... should work

 int randYFromN1To1 = 2*(Random.Range (0,2)) - 1; //this jump between -1 and 1 without touch intermediate values
 transform.position = new Vector2 ((randYFromN1To1)*screenBounds.y * 2, Random.Range (-screenBounds.x, screenBounds.x));

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

111 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

Related Questions

Using Vector3 in ViewportToWOrldPoint 1 Answer

How to make enemy prefab spawn at random times between 1.0f to 10.0f. 1 Answer

How To Random Range a Float? 2 Answers

Creating a random number of spawning items on collision 2 Answers

Prefab script values do not update 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