Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
-1
Question by ronronmx · Sep 18, 2010 at 06:10 PM · arrayfor-loopindex

Array index confusion - what does [i-1] do exactly???

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 gearSpeeds[i] = tempTopSpeed / 4 + gearSpeeds[i-1] with Debug.Log("gearSpeeds: " + gearSpeeds[i]) 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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Wotan · Sep 18, 2010 at 07:16 PM 0
Share

What exactly don't you understand? Don't you understand how arrays are indexed in general, or don't you understand how this is used in your specific application?

4 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by Jessy · Sep 18, 2010 at 07:15 PM

In short, array[i - i] gives you the value of the element at the index 1 less than i.

Your code does not generate those values. It either generates 40-160, or just 40, for index 1-4 in gearSpeeds. You obviously have something else going on in your code that's altering what you posted.

Anyway, there's no reason you have to start your for loop at 0. You could clean it up, like this:

var topSpeed : float = 160; var numberOfGears : int = 5; private var gearSpeeds : float[];

function SetupGears () { gearSpeeds = new float[numberOfGears]; for (var i = 1; i < numberOfGears; ++i) { gearSpeeds[i] = topSpeed / (numberOfGears - 1) * i; Debug.Log("gearSpeeds[" + i + "]: " + gearSpeeds[i]); } }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
-1

Answer by RSud · Sep 18, 2010 at 07:23 PM

your first time through the loop there is no value set for gearSpeeds[0] since the if statement skips setting gearSpeed[0]. I guess this is a good thing since if it didn't you first time through you would be picking up gearSpeed[-1].

So gearSpeed[0] has no value set. The coding here has a bug since you don't set any value for gearSpeed[0] but expect to use its value the second time through the loop.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jessy · Sep 18, 2010 at 07:49 PM 1
Share

This is not true. gearSpeed[0] is set to zero when the array is created. Arrays of floats are set to zero upon construction; it's not like arrays of reference types, where an element may be null.

avatar image
0

Answer by ronronmx · Sep 19, 2010 at 05:22 AM

Jessy, you're right there's something else going on in the code. Here's the full function below:

var topSpeed : float = 160; var numberOfGears : int = 5; private var gearSpeeds : float[];

function SetupGears() { gearSpeeds = new float[numberOfGears];

 var tempTopSpeed : float = topSpeed;

 for(var i = 0; i &lt; numberOfGears; i++)
 {
     if(i &gt; 0){
         gearSpeeds[i] = tempTopSpeed / 4 + gearSpeeds[i-1];
         //Debug.Log("gearSpeeds if:  " + gearSpeeds[i]);
     }
     else{
         gearSpeeds[i] = tempTopSpeed / 4;
         //Debug.Log("gearSpeeds else:  " + gearSpeeds[i]);
     }

     tempTopSpeed -= tempTopSpeed / 4;
 }

}

As you can see, tempTopSpeed is being changed at the end of the loop:

tempTopSpeed -= tempTopSpeed / 4;

Can't believe I didn't catch that, sorry for the confusion.

I'm new to coding and especially arrays, so there are a few things that confuse me. For example, why divide topSpeed by (numberOfGears -1) instead of (numberOfGears)? Is it because arrays start at 0, so in order to divide topSpeed by the correct amount of gears in numberOfGears, you have to subtract 1? Sorry if this is a stupid question...it just doesn't register in my head yet :)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by ginryu · Dec 02, 2013 at 02:12 AM

um, first off, you implementation is off. gear speed is based on a gear ration, you make a varible, or an array and make it float[] then set the index to 2 then you make a variable called gear ratio, lets say you make someFloat[0] = 20 and someFloat[1] = 40 you then have gear ratio = somefloat[0]/somefloat[1] then you take your enginepower and divide it by gear ratio,by simply changing someFloat[1] you can get a large variety of outputs. you want to also add a final gear ratio. now this system will allow you to create a very realistic engine/ transmission/ and differential setup. now i do regret having to say that a high ratio will give a weak output, by adding a torque multiplier, to the low gears that decreases as you shift into higher gears where engine power then makes up for low gear power problems. the next step is to apply a gear resistance factor to decrease eng power as pressures slow down the wheel rpms. which would cause the transmission to shift down and increasing gear torque again which compensates for the deceleration caused by climbing hills or sudden increases in drag. however i have yet to find a way to implement this plan. after i finish writing my global control script,i can once again continue testing of the system i just told you. and please, if you figure out this puzzle, i swear you will have created a vehicle control script where just changing 2 values can alter a vehicles performance quite a bit. allowing easy, and very easy to calculate car performance. ill post the code along with explicit directions on setup. also this setting will allow for very realistic rotations of gameobjects, such as drive shafts, camshafts, crankshafts. which i am actually planning on doing. control of All scripts will be done from the global control script. 1 update. and 1 fixed update. im hoping to use Time.deltaTime to apply forces, hopefully this can allow me to avoid fixedUpdate lag. i noticed that having more than a few of these drops performance radically. i must admit though, that that may be due to a mistake in my programming. my email is kristopher.j.logan@gmail.com. and if you need more info on gear ratios and the like howitworks.com is a very good resource on a large variety of subjects. search gear ratios, engines, transmissions, and differentials, torque, and horsepower. i dont understand torque calculations. i studied the content and learned a great deal, even though i was able to implement this knowledge, low gear torque still eludes me. and oh, use rigidbody.velocity.x/y/z to limit the velocity of the vehicle at low gear. oh and use TransformDirection() to switch it to global. as the very high low gear torque multiplier will cause unrealistic low gear car velocitiies. only apply this right at the drive wheels, you don't want to apply it before the differential as it will mess up animation values. ive done alot of thinking on this. and please forgive the typos. im using my android smart phones remote mouse app. as my keyboard broke:( so if this post looks unprofessional, thats why

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Error CS0029 Help? (Screenshot of Exact Error) 1 Answer

Find the index of a GameObject within an array 2 Answers

Comparing each random element in an array with each other element 3 Answers

Find specific element when duplicates exist in list. 1 Answer

Outputting data to CSV file from multiple lists at specific Headers in the CSV? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges