Vector math help, how to align a vector to a normal?

Ok, so I have made a terrain, and I am modifying the fpswalker script to my needs, and one of the issues that is bugging me is that when I walk down a steep slope, my character “jumps” down it.

I believe what is happening is since im moving the character “forward” locally, its moving forward, then gravity is taking over and pulling it down, which results in a stair stepped movement, forward, then down, forward, then down, etc.

What I would like to do, is take the object’s local forward, and side vectors and modify them, or angle them with the terrain’s normal that the character is standing on.

I don’t want to modify the angle of the character, just its trajectory. Here’s an example:

Any ideas? How would I modify the forward and side vectors so they travel along the normal?

1 Like

the side vector will remain the same.

The forward vector would be the cross product with the normal first and the right-side vector second in the arguments list.

1 Like

Thanks for the reply horsman, I will give it a shot. I believe I will need the side vector as well, as the cube (or character) could have an arbitrary rotation on the Y axis. Would this solution work still?

Either way I will give this a go and post my results.

1 Like

Right, my mistake. You’ll want to use a technique similar to the lookAt technique employed by Transform.LookAt().

Cross the forward before conversion (the one that you would have by default) with the normal, and then normalize that vector, then cross the normal with that new vector to get your forward. In short:

newRight = (trans.forward X surfaceNormal).normalized
newForward = surfaceNormal X newRight

Let me know if I screwed something up.

EDIT: Cross product order matters, of course.

2 Likes

Horsman, that worked perfectly! :smile: thanks a ton for the help! I love your brain.

1 Like

Thanks for the compliment.

1 Like