Question about code functions

HI, I have a question about the code used in the Project Boost lesson:

Ben suggests to use “(-Vector3.Forward);”
to project the booster in the opposite direction (whether you use Key A or D).

What I understand is that it is also possible to use “Back” as the obvious opposition to “Forward.”

I did this by mistake as I had paused the video before seeing Ben include the minus behind Vector3.Forward. For my brain, it made sense to use “Back” and it worked the same.

My question is (because I am new to Unity and Programming) should this give the same results in the future? Or by changing this (Opposed to Ben’s suggestion of
“-Vector3.Forward” give different results or bugs down the line?

I know there are many different ways to do the same thing, but I am curious what a more professional programmer has to say about this (just for my future understanding). I appreciate Ben and his courses and of course this site as well.

Learning lots and enjoying myself along the way!
Thank you :slight_smile:

 if (Input.GetKey(KeyCode.Space))
        {
            rigidBody.AddRelativeForce(Vector3.up);      
                }          
        if (Input.GetKey(KeyCode.D))
        {
            transform.Rotate(Vector3.back); // (-Vector3.Forward); should be same function
        }

        else if (Input.GetKey(KeyCode.A))
        {
            transform.Rotate(Vector3.forward);
1 Like

Hi Charlie, welcome to the community :slight_smile:

You ask a very good question, and to answer it, it doesn’t really make any difference, other than perhaps one of readability.

Using the minus sign in front of the Vector3.forward is just a quick way of achieving the same thing, however, I would suggest that from a readability perspective, it could lead to mistakes. In the same sense, you can check booleans for whether they are true or false like this;

if(someBool)
{
    // ...
}
if(!someBool)
{
    // ...
}
if(someBool == true)
{
    // ...
}
if(someBool != true)
{
    // ...
}

Note specifically the second example, where there is the ! character in front of the variable name. Just like the minus sign in Ben’s example, it could be easily read.

In the case of the Vector3, both forward and back are simply shortcut properties that Unity has added to save you having to type a few extra characters, e.g. Vector3(0, 0, 1) or Vector3(0, 0, -1).

My advice would be, whilst starting out and learning, write your code out in it’s longer form and then as you become more experienced and are happy with what’s what, you can start applying the shortcuts as you’ll be quick to spot them when they are being used.

I hope the above is of help :slight_smile:


See also;

Hi Rob, thank you for the quick reply. I have a better understanding having read your comment and looked over your examples. I appreciate you explaining this to me. Thank you very much! :smiley:

1 Like

Hey Charlie,

You’re very welcome :slight_smile:

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

Privacy & Terms