Why do we need to create a new vector3?

In order to move the player we need to assign a new position for the gameobject player. The thing i dont understand is why we need to create a new vector? The gameComponent “player” already has a transform object attached to it and a position. So why create a new one?

We are supposed to write:
gameObject.transform.position += new Vector3(-1,0,0);

But i would like to write something like:
gameObject.transform.position(-1,0,0);

Hey Luckie,

I’m not the most recommended person to answer this since I`m no expert, perhaps @Rob could give you a better reply. But I’ll share my point of view either way.

gameObject.transform.position is a Vector3 and returns a position inside the gamespace, it is not a method, so by writing gameObject.transform.position(-1,0,0); is the same as saying:

int myInt;
myInt(1);

or even:

int myInt;
myInt.MaxValue;

Which won`t work, since both values are {get;set;} that stores a vector3/int value after initialized, you have to use “=” to change the value, and not treat it like it is the Method() or class itself.

While the compiler won`t understand the examples above, it would understand the followings:

int myInt;
myInt = int.MaxValue

or

int myInt;
myInt = 1;  // when you use just the number, and nothing more, it is the syntax that says that the number is of the primitive type (int)

or

Vector3 MyVector;
MyVector = Vector3.zero

Vector3 is a structure class, so it has to be initialized somehow, otherwise it will be just discarded in the moment it is created and won`t be allocated at the heap memory.

Every time that you initialize a new Vector3 using Vector3 = MyVector3 (for example), you are allocating it to the memory so the script will keep the value, allowing you to access it just by MyVector3 afterwards, but if you want to change the value within the MyVector3, you will have to give a Vector3 type of value to it such as transform.position (which is a Vector3 already initialized by the GameObject itself) or even Vector3.zero (which is a Vector3 initialized inside the Vector3 class).

That’s why you have to write “new” to change the Vector3 value too, you cant just say MyVector3 = (1,0,0) since(1,0,0) is seen just as a bunch of number stacked together, and you can’t say MyVector3 = Vector3(1,0,0) because the Vector3(1,0,0) won’t be stored to the memory anyhow and is seen just as type until initialized, so you can’t pass a value that “don’t” exists. You have to say MyVector3 = new Vector3(1,0,0) or MyVector3 += new Vector3(1,0,0), when you say that it is “new”, the system will initialized it in the memory as a variable and address the value of Vector3(1,0,0) to it.

The same happens to the class Color. You can say Color MyColor = Color.red; but you can’t say Color MyColor = Color(255f,255f,255f); you would have to say MyColor = new Color(255f,255f,255f).

This is how I understand it, but lets wait for someone with more experience than me to reply.

3 Likes

Thanks alot, your replies are always so helpful! :slight_smile:

2 Likes

You are welcomed @LuckieLuuke, I’m happy to know that I’ve been helpful!

I don’t know if my reply was clear enough, did it helped to solve your doubt?

1 Like

Think you covered it perfected @Joao_Dalvi :slight_smile:

1 Like

Thank you for the answer, it helped me too.

I would also like to add “Why is it a Vector3 class and not a Vector2 class?”

At first sight, I thought it would be logical to use a Vector2 because we are working with a 2D game, but the IDE rejected the lines

transform.position += new Vector2(speed, 0f);

It took me a while to see why, but I believe it is because transform.position is a component with 3 values, meaning that I have to set all of them instead of focusing in the game mechanic. Therefore, with a “new” Vector3 the transform has all of its parameters covered.

Another point to remember is that Vector3 is a Structure Class, so it would be nice to study that concept a bit further.

I know it sounds amateur but I hope it is some progress.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.