How to Cancel Out Vector Directions

So I have a bit of an interesting situation here, but I’ll try to explain best I can what the problem is.

Say we have an object with a velocity of,-10,15) and I wanted it to stop moving according to the vertical axis… that’s easy, I’d simply add,10,0) to the original velocity, and now it’s vertical movement cancels out.

The problem I’m having, is I want to be able to do that ^ but in ANY direction. If I have a LookVector for the direction I don’t want it to move in, how do I apply it to the original velocity so that it cancels out in just that one direction?

The dot product is your friend!

Here is what you do:

  • Get the unit vector of the direction that you want the velocity to not be toward. LookVector is already a unit vector, so you’re good.
  • Get the dot product of the two vectors (in any order, so do what keeps it clear for any readers of your code). This is the speed at which the velocity vector would move something towards the look vector. This speed can be negative (i.e. the object is moving away), in which case you may want to do nothing
  • From the velocity, subtract (lookvector * speed toward lookvector) to cancel movement in that direction
1 Like

Are you trying to set all axis to 0? If so, then why not just set it to, 0, 0)?