In the 2D side scroller tutorial, the PlatformerController script contains the following line in the PlatformerControllerMovement class:
var direction = Vector3.zero;
Checking the API (and even using the convenient ability to highlight the code and press F1 to search the API) I’ve only found two items with the direction variable: the Ray and the CapsuleCollider class, neither of which seem to make sense. So what exactly is this variable? It is not declared as any particular type, so does assigning it a Vector3 value automatically assign it as a member of the Vector3 class?
Wait…I’m discovering something as I’m writing this. There is a direction variable declared within TransformDirection as a Vector3 type variable. Perhaps I’ve answered my own question, but any clarification and feedback would be appreciated. Also I know that assigning values to variables declared within the brackets() of functions is known as ‘passing arguments’ but what are the actual variables within those brackets called in programming terms?
And what exactly is a struct? I assume this is short for structure?
In Unityscript, vars are the type of whatever is assigned to them at creation unless you specifically cast them ( : Type ). Or they can be ‘anything’ if they’re just declare as “var x;”.
It’s not a member of the Vector3 class, it is a Vector3.
Variables in the parentheses () of functions are arguments.
Struct is short for structure is short for class-with-some functionality-missing-or-harder-to-get-to. Usually it’s just used to be a single container to hold a bunch of variables in and be easily passed as an argument.
“direction” is just the arbitrary name of the variable. There’s no point searching docs for variable names declared like this, since they can be anything. It could have been named “wackaWackaFoo” and nothing would change (as long as that name is consistently used elsewhere of course).
Ah, am I to assume then that the reason this variable can be declared independently from the API variables of the same name, is that it is created with the PlayerControllerMovement class and is therefore a specific member variable of that class, just as TransformDirection’s direction variable is specific to that class?
Either I’m way off, or I’m starting to understand programming.
I would suggest to write the second line instead of the first, to force the variable to adopt a single type (the desired type).
If you had written var direction = “Left”, it would have been a string then. Writing var direction : Vector3 = “Left” will give an error, and allow easier debug.