Why break up transform.position?

When creating a Vector2 for paddlePos and assigning the paddle’s position, is there a reason we are using…

Vector2 paddlePos = new Vector2( paddle01.transform.position.x, paddle01.transform.position.y);

instead of just using…

Vector2 paddlePos = paddle01.transform.position;

both seem to work fine at this stage but not sure if the shorter one will create problems later on, I doubt it will but I don’t want to assume.


Your code is valid in terms of C# but did you test it?

Yeah, I am using the code still up to designing levels so far, I kept the longer code in just commented out in case I came to any issues, but seems fine.

Was just wondering if there was any benefit in writing transform.position as a new Vector2, it seemed counterintuitive

I assume Rick wanted to stress what we are doing in this case. Actually, transform.position is of type Vector3, which also has a z-component. When we assign the transform.position to a Vector2 variable, the Vector3 objects gets implicitely converted to a Vector2 object, and the z-component gets dropped.

Explaining conversion to beginners is probably more confusing than simply passing on the x- and y-components to the Vector2 constructor.

1 Like

That makes sense. thanks for clarifying :smiley:

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

Privacy & Terms