To understand what I’m trying to do, I’ll use the analogy of “juggling” a soccer ball (trying to keep the ball in the air). Each time you kick the soccer ball to keep it from hitting the ground, you get a point, if it falls and hits the ground your score starts at 0 again…
I am doing something similar with a ball and collisions (my code applies an upward force when the ball collides with an object) but not sure where to begin to detect a series of collisions, consecutive collisions, etc., to tally my score. If the ball touches the ground, I want the score to reset…
just add a counter… every time your ball touches the colliding object to apply force increase the count by 1. if the ball hits the ground reset the counter to zero… it’s that simple.
My fault for not adding this into my example: the first time the ball collides with an object, the player gets two points. If it collides a second time without hitting the ground, their score gets multiplied by 2, a third time and the score gets multiplied by 3… So its not really counting by 1 as in your solution…
Couldn’t you just run two counters then? one for the collision and one for your multiplier? apply the multiplier to the collision counter variable. Done!
I grabbed this off of stack overflow quickly and modified it. I have a class in 45 minutes but you should be able to figure it out from here.
“counter” will always be 0 when you first call it.
“iterations” is the value of n.
“value” is your starting value.
“multiplier” is how much you want to multiply by each iteration.
public static void Recursive(int counter, int iterations, int value, int multiplier)
if (counter < iterations)
Debug.Log("The value is" +value);
Recursive(counter, iterations, (value * multiplier), multiplier);