So I am making a block game, where the goal is to shoot blocks off of a platform. Now to end the game I am attempting to make a script that tracks the y position of the blocks and add’s 1 to the counter after each reaches a certain point. The only issue I am running into, is that after they reach a certain point. They add 1, but since the function is running in function update(), it adds 1 for every frame. Is there any way to stop after 1 instead of continuing? I’ve tried everything I could think of. Heres my code for the counterfunction:
//DECLARE THESEE OUTSIDE OF THE FUNCTION SO THEY ARE NOT RESET EACH TIME
var bool1 : boolean = false;
var bool2 : boolean = false
//...and so on and so forth...
function blockFallFunc() {
var num1 = cubePos1.rigidbody.position.y;
var num2 = cubePos2.rigidbody.position.y;
//...and so on and so forth...
if(num1 < 5 bool1 = false) {
counter += 1;
bool2 = true;
}
//(!bool2) is the same as (bool2 == false)
if(num2 < 5 !bool2 ) {
counter += 1;
bool2 = true;
}
//...and so on and so forth...
}
Simplest way would be a boolean (true/false variable) that gets turned to true after the numbers exceed a certain value. Then check each tome to make sure it is false. If it is, add to the counter and set the corresponding boolean to true.
BTW, I’d recommend looking into loops and arrays as well- you could chop that down to about 10 lines.
Thanks a ton! It worked! I had the idea of using a boolean, but never thought of using it that way. How would I use a for or while loop in this situation? I tried to think of the logic behind that but I couldn’t figure out a practical way to do it. (I’m still new to coding in unity/javascript)
This would be the looping version (as a bonus, all you have to do to add a new cube is to simply add one in the inspector, whereas in your version you have to hardcode in a new variable)
private var booleanArray : boolean[];
var transformArray : Transform[];
var currentCount : int = 0;
function Awake()
{
//Set the length of the boolean array to equal that of the Transform Array when the object is created/the game starts. That way you only have to change one in the inspector
booleanArray = new boolean[transformArray.length];
}
function Update()
{
Count();
}
function Count()
{
//Declare a counter variable. This can be initiated in the for loop as well. So, it would read for(var i : int = 0; i < transformArray.length; i++)
var i : int;
for(i = 0; i < transformArray.length; i++)
{
//Check each element in the array
if(!booleanArray[i] transformArray.position.y < 5)
{
//++ adds one to the previous value; it is equal to += 1 - you just save a whole character!
currentCount++;
booleanArray[i] = true;
}
}
}
Place the GameObjects your interested in tracking in a collection of some sort (array, list, etc)
Place a trigger volume in your scene at the height you’re interested in (in your code, that looks like “5”). This must be large enough to guarantee that your objects will always collide with it when they fall.
Now, in the OnTriggerEnter() handler of said trigger, if the triggering object is in your list of tracked objects, remove the object from the list
If the list is empty - all objects have been “knocked off” - so do whatever is appropriate
That should be much more efficient as you’re only checking for a change when the OnTriggerEnter event fires instead of in the Update event - which happens every frame. And, since you remove the objects (from your tracking list) as they collide with the trigger, when the list is empty, you know that all objects have been knocked off.
I think that his way may be more efficient. Reason being you still need to check every frame whether or not the object has collided with a trigger- only your way results in you having to do a more expensive collision check, rather than simply checking if y < 5 (plus it requires additional set-up in the scene and fast moving objects could phase right through it). But, yeah, stopping the counter when all of them have been knocked off would save performance in the end.
He could also reduce the number of checks by putting the method in a co routine.
Wow! Hopefully I will eventually be able to figure out those kind of things myself eventually! Thanks a ton though. Didn’t know transform arrays even existed! Definitely a better strategy.