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?) :
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?”
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.