Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by superluigi · Jun 15, 2013 at 05:32 AM · javascriptrandom.range

How to make enemies spawn in random y position?

Hi I have a box as the child of my player and with this code it spawns enemy units every 3 seconds. What I need is for the units to spawn in a range of random y positions in side the box (the box is very tall to accommodate a wide range of positions). If not this then I can make the box smaller or switch it for an empty gameobject and have it move around in the y axis to spawn the units from there. I've read up on random.range but I am unsure on how to utilize it here. This script is attached to the spawn box. I also need advice as to what I can use to make my flying monsters fly to my players current position at the moment the spawn, but not in a homing missile type of way(dont need this scripted for me I would just appreciate some advice o what function I could use). Thank you in advance for any response.

 var hero : GameObject;
 var flying : GameObject;
 var spawner : Transform;
 
 InvokeRepeating("Spawn", 2, 3);
 
 function Update ()
 {
 var positiony = transform.position.y;
 positiony = hero.transform.position.y;
 }
 
 function Spawn()
 {
     Instantiate(flying,spawner.position, spawner.rotation);// its instattiating at 0,0,0  i need to follow this up with corrdinates    
 }
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 robertbu · Jun 15, 2013 at 05:45 AM

If I understand what you are doing correctly, you can use Random.Range() like this:

 function Spawn()
 {
     var v3 = spawner.position;
     v3.y += Random.Range(-2.5, 2.5);
     Instantiate(flying,v3, spawner.rotation);
 }

I don't know how big your box is. You need to tune the -2.5, 2.5 to the size of your box.

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 superluigi · Jun 15, 2013 at 06:43 PM 0
Share

thanks it worked but even better I learned how to use this function.

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

14 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

Related Questions

Random.Range doesn't work 1 Answer

Random.Range HELP!!! 2 Answers

trying to add rarity to items, don't know whats wrong with this script 1 Answer

Setting Scroll View Width GUILayout 1 Answer

Script sets color to multiple objects from instead of only one object with Random.range() 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