I am trying to generate a random float but I am always getting 0. I have debugged all I can think of that would make the random variable always 0 but I cannot find the problem.
This is a cloud weather type script that basically this generates a random speed (that part is not implemented yet) then takes that speed and generates a random number based on how much each weather pattern is allowed to deviate from the determined speed. Then passes those variables onto a second script which I do not think anyone will need to see but possible.
I only problem I can think of is that I am trying to get a random number based on say 0.25f or 0.75f or something like that when all the examples I have seen online were whole numbers i.e. 1f, 3f ect.
using UnityEngine;
using System.Collections;
public class CloudComponent : MonoBehaviour {
[Header("Cloud System")]
public GameObject cloudSystem; //The CloudSystem.
public GameObject cloudParent; //The CloudSystem.
public GameObject cloudParticles; //The cloud systems clouds.
public GameObject[] linkedTargets; //The linked clouds that will be chosen randomly.
public float speedMultiplier = 5f; //The speed at which each cloud will move.
public float spawnCloud; //The time between spawning clouds.
public float spawnTime = 5f; //The time it takes to spawn the next cloud.
//_____________________________________________________\\Weather Variables//______________________________________________________\\
public GameObject activeTarget; //The active target for each spawned cloud.
public float speedMinMultiplier; //The minimum speed at which each cloud will move.
public float speedMaxMultiplier; //The minimum speed at which each cloud will move.
public float speedDeviation = 0.25f; //The ammount the speed of each cloud is allowed to deviate.
public float minSpawn = 5f; //The minimum time it will take to spawn a cloud.
public float maxSpawn = 10f; //The maximum time it will take to spawn a cloud.
//________________________________________________________________________________________________________________________________\\
[Header("External System Connections")]
public AtmosphericSystem mainSystem;
//Update is called once per frame.
void Update()
{
CloudSystem();
}
public void CloudSystem()
{
spawnCloud = spawnCloud + Time.deltaTime;
if (spawnCloud >= spawnTime)
{
SpawnCloud();
//The time it takes to spawn a cloud.
spawnCloud = 0;
//Randomly set the spawn time determined by the type of weather with the min and max values defined.
spawnTime = Random.Range(minSpawn, maxSpawn);
}
}
public void SpawnCloud()
{
//Random position within this transform
Vector3 randomPosition = new Vector3(Random.Range(-1f, 1f), Random.Range(-1f, 1f), Random.Range(-1f, 1f));
randomPosition = transform.TransformPoint(randomPosition * 0.5f);
//Create the object in the randomly generated position.
GameObject newCloud = Instantiate(cloudParticles, randomPosition, transform.rotation) as GameObject;
newCloud.transform.name = "Cloud";
newCloud.transform.parent = cloudParent.transform;
//Add the cloud script to the cloud and adjust the variables accordingly.
newCloud.AddComponent<Cloud>();
newCloud.GetComponent<Cloud>().target = activeTarget;
newCloud.GetComponent<Cloud>().multiplier = Random.Range((speedMultiplier - speedDeviation), (speedMultiplier + speedDeviation));
}
}
using UnityEngine;
using System.Collections;
public class Cloud : MonoBehaviour {
//[HideInInspector]
public GameObject target; //The target that the cloud moves towards.
//[HideInInspector]
public float multiplier; //The speed multiplier for how fast the clouds move.
//Update is called once per frame.
void Update()
{
float speed = multiplier * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, target.transform.position, speed);
}
}