Search Unity

Random.Range help guys

Discussion in 'Scripting' started by nonaxanon, Dec 4, 2012.

  1. nonaxanon

    nonaxanon

    Joined:
    Jan 20, 2012
    Posts:
    44
    i need some help with this, i have a random.range var in a prefab, which acts as speed, but , all prefabs which contains this random.range var, all spawn with the same value, so random.range is not working or i dont know

    - how to spawn gameobjects , and make them have different moving speeds.

    this is basically my question , all my gameobjects move well , but i want some random speeds on them , just for the sake of variety

    if someone can help me please do, ive searched but havent found what i need, maybe its just wrong terms but its a little frustrating.
     
  2. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    one thing strikes me. Most if not all speeds would be float dependant. So, make sure you are retrieving a float value, not an int.

    this is how it should work in code.

    Code (csharp):
    1.  
    2. var minSpeed : float;
    3. var maxSpeed : float;
    4. var speed : float;
    5. speed = Random.Range(minSpeed, maxSped);
    6.  
     
  3. nonaxanon

    nonaxanon

    Joined:
    Jan 20, 2012
    Posts:
    44
    well just did this and it seems to be working bette.... i have 2 spawners, now with this tweak, the spawned ones from the spawn point B will move at the max speed and the ones from the spawnpoint A will move at the min speed. this is better, thnks a lot for this advice, i wll try to work around now . ty a lot

    EDIT:

    just fixed it , ummmm dont know why but the script in the editor was not updating , ( slaps face ) just hit reset and all changes appeared, man thank you a L O T , needed someone else's light here ujust made one puerto rican realy happy
     
    Last edited: Dec 4, 2012
  4. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    One thing I would do is increase the range. Tough in all likely hood it is a total fluke, I have noticed random range patterns that are not very random, at first. 1,1,1,1,2,2,2,3,1,... Sometimes the pattern is not complex. Increasing the range increases the chances for diversity.