Calculate how much of Vector A is in Vector B.

This is a simple question, but I can’t seem to work it out.

If my object has a rigidbody.velocity, how can I work out how much of that velocity is moving in the same direction of transform.forward?

I think it has something to do with Vector3.Project… any ideas?

I’m not too sure about project (it’s description sounds like it might do the job, but it uses an “onNormal” which I’m not sure how that affects it). Short of that, you could probably use the Dot Product of them. If you use the dot product on the unit vector equivalents of the two directions, I think it should give you a number between -1 and 1 (-1 being backwards, 1 being exactly forwards, 0 being perpendicular; essentially the cosine of the angle between them). But my math is a bit rusty there so I may be off.

EDIT: if you want, create a test script that runs on a sample of hard-coded vectors and see how the math would works out for you. Maybe play around with the Project.

EDITx2: Oh, I see now that the project method is static. So the “onNormal” is likely the target vector (I’m guessing a unit vector) that you want to project onto. So I guess if you calculate the magnitude of your projected vector and compare it to the magnitude of your original velocity vector, you’ll get an idea of how “off” they are. (though you would still need to account for direction – could be the same magnitude but pointing in opposite directions). The greater the difference in magnitude, the more perpendicular (off-centre) they are. The closer the magnitudes are together, then the more parallel (on-target) the velocity is.

You are correct.

Use Vector3.Project(rigidbody.velocity, transform.forward). This will give you a new Vector3 that is the part of velocity that is in the direction of forward. If the result is Vector3.zero, the velocity is 0 or not at all in the direction of forward (perpendicular). If the result is rigidbody.velocity, the velocity is entirely in the forward direction (or entirely in the opposite direction!).

In general the way to find the component of Vector A that is in the direction of Vector B is to use the dot product (the dot product is awesome):

  1. copy B and normalise the copy. This is now a unit vector (length 1.0). Let’s call it vB_unit;

  2. do the dot product of vB_unit against A and store the result which will be a float, not a vector. Let’s call this fADotB_unit.

This value is equivalent to the length of Vector A that is in the direction of Vector B.

The order af the vectors doesn’t matter for dot product because it’s a commutative operator (look it up if you don’t know what that means :slight_smile: ).

  1. scale vB_unit by fADotB_unit and the resulting vector is the component of A that is in the direction of B.

N.B. Normalising a vector involves doing a square root, so is a relatively slow operation; you only need to do this if Vector B isn’t already a unit vector.

BTW, I guess this is what Vector3.Project() does internally, so it’s probably more optimal to call that than do it longhand as I suggest above :slight_smile:

No idea if that helps, but I hope so.

darbotron

I don’t know if the answer is correct, but i like it :smile:

Wow thanks, you guys are awesome!

I have many options to try!

-Sw,