Both Vector3
And Transform
are “classes”. A class is like a group of different variables and functions that do different tasks. Vector3
is a class that consists of three main variables: x, y and z. These three variables are floating point numbers (numbers that can contain decimals, in programming they are specified as float
) and can be anything you specify.
Every class is a kind of object. To create an object in almost all programming languages you have to use the new
keyword and the class you want to create, which will look like this: new Vector3 ()
.
Declaring variables is a very important part of programming. A variable can either be a class (like Vector3, Transform) or a data type like
int (only whole numbers like 1, 2, 3),
float (floating point numbers like 1.0, 2.34, 1)
bool (short for boolean, is either true or false)
There are other data types, this is just a few.
To specify that you want to create a variable you type in the variable type (either class or data type) and then the variable name. A couple examples: float myvariablename;
, Vector3 myVector;
To set a variable value you can either do it in one line like this: float myvariablename = 1f;
or like this:
float myvariablename;
myvariablename = 1f;
Notice that I used an “f” at the end of the number. This is just something you have to do with floats. Just type in the decimal number you want like 1.33 and add the f (if you don’t you will probably get an error).
You asked why you can’t set a variable value like this: Vector3 pos = (0, 2, 0);
This is because you didn’t specify the class to be created. That’s why you have to add in new Vector3
.
You also said that you didn’t understand what the code meant. Let’s take a look at it line-by-line:
Vector3 pos = transform.position;
Here you are creating a variable called pos and the type is a class named Vector3. You are assigning it a value of transform.position
, which is a variable containing the x, y and z position of the current object the script is attached to.
pos = new Vector3(0, 2, 0);
You are giving the variable pos a new value which is a new Vector3 with the x value of 0, y value of 2 and z value of 0.
Transform tran = transform;
Creating a new class of type Transform called tran. You are assigning it the value of the variable transform which is the position, rotation and scale of the object the script is attached to.
tran position = new Vector3(0, 2, 0)
I don’t know what you did here. This code is written wrong and would give you an error. “tran position” means nothing at all. If you removed the “position” you would give the value of the Transform tran a new Vector 3 of xyz (0, 2, 0), but this is also illegal (will throw an error), because the variable tran is a Transform, meaning that you can not assign it a Vector3 (since they are not the same class)
This was a lot of information to read through, and programming is hard to understand at first, but after a while you will get the hang of it. I hope this answers helped you and I would gladly assist you further if you need more help. Just add a comment to this and inside the comment you write “@rubenchris” (without the two ") at the end of the comment, so that I will get a notification