I’m having a hard time understanding the “gearSpeeds[i-1]” part of the for loop below. Isn’t [i-1] equal to current array index - 1?
var topSpeed : float = 160;
var numberOfGears : int = 5;
private var engineForceValues : float[];
private var gearSpeeds : float[];
function SetupGears()
{
engineForceValues = new float[numberOfGears];
gearSpeeds = new float[numberOfGears];
var tempTopSpeed : float = topSpeed;
for(var i = 0; i < numberOfGears; i++)
{
if(i > 0){
gearSpeeds[i] = tempTopSpeed / 4 + gearSpeeds[i-1];
Debug.Log("gearSpeeds: " + gearSpeeds[i]);
}
When I debug the "gearSpeeds = tempTopSpeed / 4 + gearSpeeds[i-1];" with "Debug.Log(“gearSpeeds: " + gearSpeeds*);” it gives me the following results:* gearSpeeds: 70 gearSpeeds: 92.5 gearSpeeds: 109.375 gearSpeeds: 122.0312 But if I remove “+ gearSpeeds[i-1]” and debug again, I get: gearSpeeds: 30 gearSpeeds: 22.5 gearSpeeds: 16.875 gearSpeeds: 12.65625 What I can’t figure out is how “gearSpeeds[i-1]” equals 40 in the first index (30+40=70)??? I’m finally starting to understand Arrays and looping through them with for loops, but the [i-1] results are making my brain smoke…lol
You were right, tempTopSpeed is being changed at the end of the loop:
tempTopSpeed -= tempTopSpeed / 4;
I should have seen this earlier…it’s hard being a noob to coding, thank god for ppl like you willing to help
So if I understand correctly, then the value of tempTopSpeed is being multiplied by 0.75 after the if/else statement gets executed?