- Home /
Bulletin Array, change element variables
I am making a game of tanks, which therefore involves a lot of wheels. Rather than make a new script for each vehicle I want to make just one flexible script.
I have gotten as far as making a bulletin array, but I don't know how to change the torque of wheels when they are inside an array.
var wheelsleft:WheelCollider[];
function Start(){
}
function Update () {}
Answer by Adam-Buckner · Sep 08, 2011 at 08:14 PM
To change the values within an array, you must "iterate" through it (or over it - both seem to be correct terms).
A common way to do this is with a "for" loop. You can also do a "foreach" loop.
// This will print the Game Object name of everything in the array:
var myArray : GameObject [];
for (var i : int = 0; i < myArray.Length; i++) {
Debug.Log (myArray[i].gameObject.name);
}
// You can include many operations within this for loop, including tests:
for (var i : int = 0; i < myArray.Length; i++) {
if (myArray[i] == someValue) {
myArray[i] = someNewValue;
}
}
// This will go through all of the contents of an array and do something to it.
// In this case it will turn all of the red lights off:
var myLights : Light [];
foreach (light : Light in myLights) {
if (light.color == Color.red) {
light.enabled = false;
}
}
In your case, you will need to run through all of your wheels with a for or foreach loop and do something to it...
The only note I would throw out there is: Do you need to work all the wheels? Can you "drive" your tanks in a different manner, and then simply run the wheels as a visual effect?
A note on how these work:
The "foreach" is pretty obvious: "For each light in lights, if the light is red, turn it off".
The "for" loop is a little more arcane. There is the for loop "for (something)"... Something in this case it the incrementation of an int. This is saying: "Take a value, say "i", and set it to 0. If "i" is less than the length of my array, keep going - otherwise stop. When the loop is over, increment "i" by one. Now, in the block of code, the code is looking for a specific "slot" in the array when it looks at myArray[slot number]. Because "i" starts at 0, and then increments up by 1 each pass of the loop, by asking "Look at myArray[i];", you are saying "Look at myArray[0];" and then it loops, adds one to "i", and then you are asking "Look at myArray[1];". When "i" is greater than the length of the array, the loop stops. This way it looks at all of the slots in your array.
Thanks for the reply, I didn't find the script refrence thorough enough. I'll be back to with the code when its going.
Yes I could power just 4 wheels and have some dummies, or forgo the wheels for some kind of transform or raycast. But making lots of wheels in a line will give more of a track effect and powering all of them will make sure theres traction wherever the tracks are touching.
Don't forget that you can drive your vehicle with wheel colliders that are not parented to any visible world object... and then have ALL the wheels dummies. You might even be able to get rid of wheel colliders altogether, and drive the vehicle some other way, tho', admittedly, one set of wheel colliders might be useful, maybe just 2 in the middle of the tank? If it works, it doesn't need to mimic real life.