Vector 3

Hi!
Why do we have to use Vector3 and not Vector2 in the update method (Vector3 delta = rawInput;)?
Thanks in advance.

Hi,

Have you already tested your idea? If not, do that, please, and let me know what you figured out. In many cases, there are multiple ways to do things in Unity, and Gary cannot show all of them.

Hi Nina,
I tried vector2 and it did not work, but that is not the point of my question.
It was the first time we used vector3, and it was without any explanation. Why now and not before?

Oh, I see. I thought you wanted to know why we didn’t use something that doesn’t work.

Unity is a 3D engine. Even 2D is 3D where the z-position is usually set to 0, and the camera to “orthographic” mode. transform.position is always of type Vector3, even in “2D” games.

Vector3 contains 3 floats: x, y, z. Vector2 contains 2 floats: x, y. Usually, it is possible to assign a Vector2 value to a Vector3 variable. The z-value becomes 0 in that case. And if you assign a Vector3 value to a Vector2 variable, the z-part gets “dropped”.

The conversion usually happens implicitely. For this reason, I’m wondering what you tested and how you tested it.

Vector2 delta = rawInput * moveSpeed * Time.deltaTime;
transform.position += delta;

should have worked as well.

I assume Gary used Vector3 because of transform.position, which is of type Vector3. If you need a z-position that is not 0f in your game, always use Vector3. If a z-position of 0f is fine or crucial, you may use Vector2.

Is this what you wanted to know?

1 Like

Thanks!
I really appreciate your answer. That was exectly what I wanted to know.
To tell you the truth, I tried to put vector2, saw red squiggly lines, and immidiatly changed it to vector 3, so I guess there was something else causing the problem.
Thanks again!(:

Maybe I was wrong about the implicite conversion from Vector3 to Vector2. I’ve just checked the API and wasn’t able to find any information. Vector2 to Vector3 works implictely, though.

If the implicite casting does not work, we would have to cast explicitely:

Vector2 delta = (Vector2)(rawInput * moveSpeed * Time.deltaTime);
transform.position += delta;

This example does not make much sense because delta will get converted to Vector3 because of transform.position.

2 Likes

Thanks Nina!

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

Privacy & Terms