0
\$\begingroup\$

I was making a 3rd person player controller in Unity.

Would

Vector3 inputDirection = orientation.forward * verticalInput + orientation.right * horizontalInput;

be the same as

Vector3 inputDirection = new Vector3(horizontalInput, 0.0f, verticalInput)

?

I'm pretty new to Vectors, and would like help understanding how or how they aren't the same.

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

This expression:

inputDirection = orientation.forward * verticalInput + orientation.right * horizontalInput

can only equivalent to this:

inputDirection = new Vector3(horizontalInput, 0.0f, verticalInput)

if orientation.forward is Vector3(0.0f, 0.0f, 1.0f) and orientation.right is Vector3(1.0f, 0.0f, 0.0f).

Which I cannot guarantee to be true. Why? Because orientation is a variable, and thus it might… well… vary. Using orientation allows you to have the input be… well… oriented differently. For example, to make it relative to move a character in a direction relative to where the camera is pointing or to move a vehicule according to its current orientation, and so on.


Ok, so you say you want to understand vectors. You need to start with these operations:

  • Vector-Scalar product
  • Vector addition

Vector-Scalar product

So, when you have a Vector-Scalar product such as this:

v * f

Is equivalent to this:

Vector3(v.x * f, v.y * f, v.z * f)

Thus, this:

orientation.forward * verticalInput

Is equivalent to this:

Vector3(
    orientation.forward.x * verticalInput,
    orientation.forward.y * verticalInput,
    orientation.forward.z * verticalInput
)

And this:

orientation.right * horizontalInput

Is equivalent to this:

Vector3(
    orientation.right.x * horizontalInput,
    orientation.right.y * horizontalInput,
    orientation.right.z * horizontalInput
)

Vector addition

If you add two vectors like this:

a + b

It is equivalent to this:

Vector3(a.x + b.x, a.y + b.y, a.z + b.z)

So when you have this:

Vector3(
    orientation.forward.x * verticalInput,
    orientation.forward.y * verticalInput,
    orientation.forward.z * verticalInput
) + Vector3(
    orientation.right.x * horizontalInput,
    orientation.right.y * horizontalInput,
    orientation.right.z * horizontalInput
)

It is equivalent to this:

Vector3(
    orientation.forward.x * verticalInput + orientation.right.x * horizontalInput,
    orientation.forward.y * verticalInput + orientation.right.y * horizontalInput,
    orientation.forward.z * verticalInput + orientation.right.z * horizontalInput
)

Wrapping up

So how can

Vector3(
    orientation.forward.x * verticalInput + orientation.right.x * horizontalInput,
    orientation.forward.y * verticalInput + orientation.right.y * horizontalInput,
    orientation.forward.z * verticalInput + orientation.right.z * horizontalInput
)

Be the same as this:

inputDirection = new Vector3(horizontalInput, 0.0f, verticalInput)

Well, it implies that:

horizontalInput == orientation.forward.x * verticalInput + orientation.right.x * horizontalInput

0.0f == orientation.forward.y * verticalInput + orientation.right.y * horizontalInput

verticalInput == orientation.forward.z * verticalInput + orientation.right.z * horizontalInput

And thus:

orientation.forward.x == 0.0f
orientation.right.x == 1.0f

orientation.forward.y == 0.0f
orientation.right.y == 0.0f

orientation.forward.z == 1.0f
orientation.right.z == 0.0f

Which is how I get this:

orientation.forward = Vector3(0.0f, 0.0f, 1.0f)

orientation.right = Vector3(1.0f, 0.0f, 0.0f)

Learning material

I'm going to send you to 3Blue1Brown video series on Linear Algebra.

I have written an Introduction to Vector Algebra and How to Work With Arbitrarily Oriented Vectors before, which you go over if you rather my writing.

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.