1
\$\begingroup\$

I've noticed numerous tutorials and examples using something like "transform.up * -1" or "-Vector3.up" instead of simply saying "transform.down" and "Vector3.down".

Is there a good reason for this, or is it simply a style difference?

I'm specifically thinking about downward raycasts, if that matters at all.

\$\endgroup\$
  • \$\begingroup\$ Not really, both cases whether transform (locally) or Vector3 (world) mean the inverse if you change their signs. Although it is better to call for -Vector3.up than (Vector3.up * -1f), because the later needs a multiplication operation. Asides from that, it may be just comodity, pecause it is easier to change code, than to change "up" for "down", for example. \$\endgroup\$ – LifGwaethrakindo Mar 19 '18 at 20:29
  • 2
    \$\begingroup\$ Note that while Vector3.down exists, transform.down does not. (At least, not according to the docs as of version 2017.3) \$\endgroup\$ – DMGregory Mar 19 '18 at 22:26
2
\$\begingroup\$

They're equivalent. You can verify via the documentation for down and up respectively.

The choice is thus down to style (or perhaps ignorance of the existence of down) and relatively minor, nitpicky sorts of details (like how one could make that case that, for iteration purposes, it may be easier to add or remove the leading - to switch between two options).

\$\endgroup\$
1
\$\begingroup\$

Vector3.up equals to new Vector3(0, 1, 0)

Vector3.down equals to new Vector3(0, -1, 0)

When you do -Vector3.up it sets every value to negative value:

public static Vector3 operator -(Vector3 vector)
{
    vector.x = -vector.x; 
    vector.y = -vector.y; 
    vector.z = -vector.z;

    return vector;
}

Or Unity could also implement it as immutable type and it will do:

public static Vector3 operator -(Vector3 vector)
{
    return new Vector3(-vector.x, -vector.y, -vector.z);
}

Though, you can do someVector.y = 10f;, thus they are not immutable.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.