remove forward component from velocity vector

Got a model with a rigidbody, a move script and a basic raycast.

When the raycast comes into contact with an obstacle, i want the model to stop “forward” (the unit’s nose direction) velocity only. I want all other directional velocities to be unaffected.

when i say forward i do mean the objects own ‘forward’. if the model is falling or moving diagonally, it’s still only the forward i want to affect, not any other movement

JUST stop velocity in the direction of the nose of the object.

Halp?

Tom :slight_smile:

so you need to have good primary school Vector math here ! :slight_smile:

you have your velocity V.

you need the FORWARD COMPONENT of V.

by “forward” i mean the way your guy is pointing – or indeed, any direction you are interested in

to get that use this.

Documentation/ScriptReference/Vector3.Project.html

if you don’t know what “projecting a vector” means simply google it for 1000s of pages of diagrams and explanations

so that will give you the Forward Component of velocity, FC

next simply take V and subtract FC

that will be your new velocity. Set it !

or the no bullshit version …

rigidbody.velocity
    = rigidbody.velocity - Vector3.Project(
                rigidbody.velocity, transform.forward )

amazingly unity makes it that simple.

don’t forget if it’s moving perfectly straight ahead this will of course stop it.

Is this supposed to be some sort of collision solution? Because these sort of methods don’t tend to work out very well.

Why does it have to be a raycast? Does it really have to have an infinite range? If not why not use a collider?
Same thing about why does it have to be a line? If not, a collider will be better as well.

If it needs to be infinite, and is more like a “beam that pushes us away from things” why not use a force on a rigidbody?