Vector3 explanations

Hello everyone,
I am new in unity and i follow and asteroids tutorial.
I have 2 questions about vector3.
The tutorial i follow is in 2d and i don’t understand why Vector3 are used.

First in my script “AsteroidSpawner” i don’t understand these 2 lines (is Vector3 used to declare a variable?) :

Vector3 spawnDirection = Random.insideUnitCircle.normalized * this.spawnDistance;
Vector3 spawnPoint = this.transform.position + spawnDirection;

I also have some difficulties to understand these 2 lines in the scipt “asteroid” (why a Vector3 is used to set an angle but in 2d?):

this.transform.eulerAngles = new Vector3(0.0f, 0.0f, Random.value * 360.0f);
this.transform.localScale = Vector3.one * this.size;

unity game objects will have 3D positions (X,Y,Z) even if you make 2D game.

vector3 is the type of variable spawnDirection, and gets assigned that random vector3 value:

Vector3 spawnDirection = Random...

this creates new vector3, with x,y,z the Z is that “Random.value * 360.0f”

.eulerAngles = new Vector3(0.0f, 0.0f, Random.value * 360.0f);

also see Vector3.one, its same as new Vector3(1,1,1)

Ok i think i will need time to fully understand Vectors.
Apparently they can be used to set positions, angles, directions…

for info, i have my big collection of vector3 and related math links here:

(search vector for those specific tutorials, guides)

1 Like

I think it’s just a matter of learning how computer programs tend to think. Variables are strict about their data type, but don’t care about the human-meaning-type. For example a float variable (which is just a number) can stand for age, hat-size, distance … or anything else which is one number. When you see float n; you wouldn’t assume it’s someone’s age. Vector3 is the same way. It’s simply a group of 3 numbers labelled x,y,z. That happens to work for position, scale, and angle-rotation. It also works for funnier stuff like the size of an area or the distance between 2 objects. Or sometimes it’s flat-out abused in stuff like x=cats, y=dogs, z=fish.

So when you see Vector3, it doesn’t stand for anything. It’s more the other way around: you know transform.localScale has to be an x,y,z; the system has Vector3 lying around; so they used it for localScale.

An obvious drawback of this system is you can copy wrong things by mistake, like putting localScale into a slot for position. There are ways to fix that by creating a type for each: like an “ounces float” and a “pounds float” and if you added them by mistake (3 ounces plus 4 pounds = 7 what???) it either gave an error, or auto-converted ounces to pounds. But that wasn’t as good as we thought. So now we’re “Vector3 is 3 floats. Anytime you need 3 floats stuck together, why not use it?”

3 Likes

Vector3’s are really just 3 floats, and you’ll see them used really any time you need 3 related floats. What they mean depends on context, but you’ve mentioned the most common uses.

2 Likes