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 /
avatar image
1
Question by Seventh Stealth · Feb 17, 2014 at 12:49 PM · randomrandom.rangefunction update

Random.Range only returns lowest value

 function Update() {
 var speed = Random.Range(1,3);
 if(speed == 1){
 this.transform.Translate(Vector3(500,0,0) * Time.deltaTime);
 }
 if(speed == 2){
 this.transform.Translate(Vector3(30,0,0) * Time.deltaTime);
 }
 if(speed == 3){
 this.transform.Translate(Vector3(100,0,0) * Time.deltaTime);
 }
 }








So I've got this script . There's three spawn points and obviously the spawn blocks . This script is attached to the blocks and is meant to varie how fast they fly . The only problem is that it only ever returns as 1 . I've tested with deifferent speds other than 500 and that's all it does . Return 1 . How could I fix this so it returns 2 and 3 as well ?

Thanks, Stealth .

Comment
Add comment · Show 4
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 Anusha · Feb 17, 2014 at 12:53 PM 2
Share

if you want the random number to be 1,2,3... then var speed = Random.Range(1,4);

avatar image AlucardJay · Feb 17, 2014 at 01:08 PM 0
Share

As Anusha has stated, but to clarify. Check the Unity Scripting Reference : http://docs.unity3d.com/Documentation/ScriptReference/Random.Range.html

  • floats : Returns a random float number between and $$anonymous$$ [inclusive] and max [inclusive]

  • integers : Returns a random integer number between $$anonymous$$ [inclusive] and max [exclusive]

avatar image Berenger · Feb 17, 2014 at 01:09 PM 0
Share

Try a loop from 0 to, say, 1000. Use print(Range(1, 3)); to see for yourself that is output is either 1 or 2. Then try print(Range(1, 4));

avatar image Seventh Stealth · Feb 17, 2014 at 02:09 PM 0
Share

it's printing 1 ,2 and 3 but the speed isn't changing

1 Reply

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

Answer by wibble82 · Feb 17, 2014 at 02:19 PM

Hi

First up, Random.Range can work in 2 ways - either with integers or floats. If used with integers like you're doing, it returns a number from min, to max-1. So your code will only ever get a speed from 1 to 2, never 3.

However, seeing as you're getting a new random number every frame (in the update) you will over a period of time end up generating roughly the same number of 1s as 2s, so the object will appear to move at a constant speed (even though each frame its moving by a randomly different amount).

If what you want is to pick a speed when the object is first created you'll need to make the 'speed' a member of the class, and initialise it just once inside the Start function. That way each object will pick a random speed it should move at, then move a fixed amount each frame.

(edit) fyi, if you want genuinely random speeds, you might try var speed=Random.Range(30.0,500.0) to get a totally random number from 30 to 500. Then each frame translate by Vector3(speed*Time.deltaTime,0,0)

(and another edit) oh, and given you're messing with the 'physics' of the object, you probably want this code to be in the 'FixedUpdate' function, and use Time.fixedDeltaTime. FixedUpdate runs in tandem with the physics engine's update and so is a better place to put code like this that wants to move an object at a fixed rate. Won't have any obvious affect right now, but its good practice and will save you pain in the long run!

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 Owen-Reynolds · Feb 17, 2014 at 05:09 PM 0
Share

$$anonymous$$ain thing, paras 2 and 3. Pick one speed for each object and stick with it. Once you randomly pick the starting speed, there's no more random for that guy.

That's different from (and harder than!) random every frame. And takes a code rewrite (para 3.)

avatar image Seventh Stealth · Feb 20, 2014 at 11:09 AM 0
Share

Thanks wibble and Owen :D Scipt works perfectly now :]!

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

23 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

Related Questions

Lasers Fire over and over 2 Answers

Random.Range(..) not working 1 Answer

Non-repeating random number generator crashing unity 1 Answer

How do you get different random numbers for each object array? 1 Answer

Random.Range 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