- Home /
while loop & array- turn on something via percentage
Hello Everyone,
I am currently creating a script that makes rocks fall one at a time by the amount of percentage is complete.
The problem is, it can only be done on very specific numbers. (currently = modulus has to equal zero to turn on falling rock). while the script is able to handle any number of rocks, if I change the speed of how fast the percentage goes up, it can skip rocks, making them fall out of order, or not at all.
Is there a way I can re-write the while loop to have it drop each rock based off of percentage and the number of rocks I have?
The while loop is under the refillRate() method. Here is my script:
#pragma strict
var rate : float;
var curProgress : float = 0;
private var maxProgress : float = 100;
private var drawGUI = false;
private var percentOfProgress : float;
var progressBarLength : float;
private var maxProgressBarLength : float = 100;
var progressTexture : Texture2D;
var progressTextureMain : Texture2D;
var canDecrease : boolean = false;
//var screenup : float;
//var screenside : float;
var excavateStyle : GUIStyle;
var rocks : Rigidbody[];
private var check : boolean = true;
private var i : int = 0;
private var percentage : float;
function Start ()
{
percentage = (100/rocks.length);
}
function Update ()
{
if (drawGUI == true && Input.GetKey(KeyCode.E))
{
refillRate();
}
else if (canDecrease == true)
{
defillRate();
}
percentOfProgress = curProgress/maxProgress; //current % in decimal
progressBarLength = percentOfProgress*100; // current %
if (curProgress > maxProgress)
{
curProgress = maxProgress;
}
if (curProgress < 0)
{
curProgress = 0;
}
}
function refillRate()
{
curProgress+= rate/100; rocks[0].isKinematic = false;
while(progressBarLength % percentage == 0)
{
//rocks[i] = GetComponent.<Rigidbody>().isKinematic = true;
if (check == true)
{
check = false;
i++;
rocks[i].isKinematic = false;
yield WaitForSeconds (0.1);
check = true;
}
else
{
Debug.Log("out of range/time");
}
if ( i >= rocks.length-1)
{
i = 0;
}
}
}
function defillRate()
{
curProgress-= (rate/100)/2; //fills up the progress meter.
}
function OnTriggerEnter (theCollider :Collider)
{
if (theCollider.tag == "Player")
{
drawGUI = true;
}
}
function OnTriggerExit (theCollider :Collider)
{
if (theCollider.tag == "Player")
{
drawGUI = false;
}
}
function OnGUI()
{
if (drawGUI == true)
{
GUI.Box (Rect (Screen.width*0.5, Screen.height*0.3, 300, 45), "Hold E to Excavate",excavateStyle);
GUI.DrawTexture(Rect((Screen.width/2.5), Screen.height/10, maxProgressBarLength *Screen.width/650, 50), progressTextureMain);
GUI.DrawTexture(Rect((Screen.width/2.5), Screen.height/10, progressBarLength *Screen.width/650, 50), progressTexture);
}
//calculate bar length, based off of how much is completed.
}
Answer by JoeStrout · May 24, 2015 at 02:53 AM
Yes -- your code here is working much too hard. Don't use coroutines; they'll just confuse you. Simple keep track of how much "percentage" (work of some sort?) still has to be done before you spawn the next rock. Update this in your Update() method, and when this becomes
No need for while/yield, and no possibility of skipping a rock or spawning them out of order.
the problem is that each scene has a different number of rocks in them, some have 6 while some can have 20.
by using the while loop/array, I can have it check each one individually, ins$$anonymous$$d of me having to make a new script for scene telling it to turn off each one at a set percentage.
it's not spawning rocks either, it just turns off is$$anonymous$$inematic to make them fall again on objects already in the scene.
But as you stated "Simple keep track of how much "percentage" (work of some sort?) still has to be done before you spawn the next rock" this is what I'm trying to accomplish with the script. I'm trying to find a way for the script to calculate how much percentage in increments would be needed before the next rock should fall, while taking into account the array.length.
@JoeStrout After I took a break and re-read what you said, it finally clicked. Think I've been staring at the computer too long for it to process. But I took your advice and changed out the while loop with an if statement. Works like a charm now! Here is what I did to fix it.
I changed the refillRate() method.
it is now:
function refillRate()
{
curProgress+= rate/100; //fills up the progress meter.
if ( progressBarLength >= percentage*(i+1) ) //checks how many objects we have, and then increases percentage needed to drop next object.
{
if (check == true)
{
check = false;
rocks[i].is$$anonymous$$inematic = false;
i++;
yield WaitForSeconds (0.1);
check = true;
}
else
{
Debug.Log("out of range/time");
}
if ( i > rocks.length-1)
{
i = 0;
}
}
}
Looks good! But you should no longer need the yield WaitForSeconds bit.... nor all that stuff about "check"... there's more cruft here you can cut away, but you've basically got it!