Help with raycasting and CrossProducts

So I got this animal working. Which makes it lock on too terrain surface. But unfortunately It can’t be moved in any direction except the one it is already facing. Which means when the character moves. It will always be facing this direction. Is there a possible fix for this?

The code I am currently using to make this work.

1 Like

The way the cross product works is it returns a vector perpendicular to the two you put in. So, you have the normal of the terrain, which is going kind of sideways-and-up. Then you have the Up vector which is,1,0). Using right-hand rule we know that the resultant, which is facing stage right (of the video). The model will always face left on the hill because you are crossing with the up vector.

Here’s my suggestion: instead of crossing against the up vector, you cross against the orientation of the model itself. This will give you the best approximation of the model’s CFrame while making its Up direction face the direction of the hill.

You can do this by creating 3 orthagonal vectors, Binormal, Normal, and Tangent – the X, Y, and Z axis respectively – and create a CFrame out of those component vectors (plus position). To do that, you can use two cross products.

Stage 1: cross PrimaryPartCFrame.rightVector with raycast Normal. This produces the Tangent
Stage 2: cross raycast Normal with Tangent to create the Binormal

You can then create a CFrame with the components of these vectors plus position components, like so:, Position.Y, Position.Z, Binormal.X, Normal.X, Tangent.X, Binormal.Y, Normal.Y, Tangent.Y, Binormal.Z, Normal.Z, Tangent.Z)

I basically tried doing what you said. And it works except for it doing this. And I’m not sure what is causing it.

Your Binormal is backwards, it needs to be Normal cross Tangent, not the reverse. If cross in the reverse order, it gives you a vector that’s facing the opposite way, and that is what is happening: the CFrame that’s being constructed is facing the opposite way from the original.