Hey Unity! The Vector3
is a mutable data type with respect to its components. If you have, say,
Vector3 a = Vector3.up;
Vector3 b = Vector3.right;
You can get the additive resultant as either
return a + b;
or
return new Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
but which one is faster? I’m inclined to think that the internal implementation of the first option is just the second option, and in any case I doubt if the first option can be slower than the second, but the docs for the addition operation don’t specify, and as far as I know it is technically possible that it’s instead implemented
Vector3 operator+(const Vector3& other) {
this->x += other.x;
this->y += other.y;
this->z += other.z;
return this;
}
which should be faster than the second option since you’re not going out of your way to allocate memory for a brand new Vector3 object.