• 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 FH_88 · Jan 11, 2015 at 03:31 AM · javascriptgameobjectpositionrandomspawn

Spawn game object in random position on screen

I have a script which spawns a game object every fourth second, but I would like the objects, to spawn in random positions in a specific area, to make sure it won't appear off screen. How would I do this?

This is my script.

 var Mines : Transform;
 
 private var Timer : float;
 
 function Awake () {
     Timer = Time.time + 4;
 }
 
 function Update () {
     if (Timer < Time.time) {
         Instantiate(Mines, transform.position, transform.rotation);
         Timer = Time.time + 4;
         Debug.Log("Spawned Mine");
     }
 }
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
0
Best Answer

Answer by Cherno · Jan 11, 2015 at 03:56 AM

You could either...

use a random Camera.ViewportToWorldPoint (if it's a straight top-down view, as you will have to define the distance of these points from the camera),

or define the boundary x and z values (in world space) of your playing field if there's no scrolling involved, and then just roll random x and z values for the transform.position (I assume that y is always 0 = ground), but Mathf.Clamp them between the min and max x and z boundary values,

or, for the most flexibility but most complicated way, cast rays from the four corners of the camera view (= viewport) against the ground layer (again with Camera.ViewportToWorldPoint, but with a z values of 0), then use the hit.points as your min/max boundaries.

Comment
Add comment · Show 2 · 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 FH_88 · Jan 11, 2015 at 06:54 PM 0
Share

Great answer, thank you very much.

avatar image Cherno · Jan 12, 2015 at 01:01 AM 0
Share

I just noticed that in my second suggestion, the $$anonymous$$athf.Clamp isn't needed because the Random.Range function already takes parameters for $$anonymous$$ and max values :) A fourth possibiltiy that gives more control over the spots where objects can spawn would be to have an array or list of Vector3s (or GameObjects) that are scattered over the landscape by the level designer, and then a random number is rolled for the position that is pulled from the array/list. (Empty) GameObjects can be used ins$$anonymous$$d so the level designer can see where his spots are, otherwise he would just have the plain V3s without visualization.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

26 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

Related Questions

Generate random number and set a GameObject active. 1 Answer

Spawn random amount of gameobjects 2 Answers

Spawn random amount apart of each other 1 Answer

GameObject variable change for all of the objects 1 Answer

Slender pages collection does not activate gameobject I want 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