Or not to. C#

I want my random.range script choose between 1 or 2 position not 1 to 2 positions, how to do it? Can i do it with Random.Range?

Here’s my script, it’s working but not the way i want.

using UnityEngine;
using System.Collections;
public class ObstacleSpawnScript : MonoBehaviour {
    public GameObject Car1;
    public Vector3 spawnLocation;
    // Use this for initialization
    void Start () {
        StartCoroutine (ObstacleSpawn ());
    }
    IEnumerator ObstacleSpawn(){
        while (true) {
            spawnLocation = new Vector3(10, -0.5f, Random.Range(-1.55f, 1.55f));
            Instantiate(Car1, spawnLocation, Quaternion.identity);
            yield return new WaitForSeconds(4f);
        }
    }
}
(Random.value > 0.5f ? -1.55f : 1.55f)

Not directly, “range” has a pretty specific meaning, but you can use the same percentage-chance concept used in most loot systems with a simple if-statement. For instance, just do Random.Range from between 0 and 100, and then if the number if less than 50, set a variable to -1.55f, and if it’s over, then set it to 1.55f. Using the same setup, you can make the chances lopsided by doing, for instance, 1-30 (30% chance) and 31-100 (70% chance).

Make an array of potential positions, Randomly select one from the array.

1 Like

If you pass integers to the random function, you are getting an integer as result. If you use an array, you random call might look something like that:

Random.Range (0, theNameOfYourArray.Length);