How to face a part to the surface hit from a Ray?

Hello! I’ve been trying to figure out how to face a part to the surface that a ray has been hit.

To understand better, here’s a diagram of the desired outcome:

Not really good at advanced CFrames, I’ve tried using CFrame.lookAt() but I do not know what variables to put in.

I’ll be using this for actions like wall-jumping, and things a like.

Any help is appreciated!

1 Like
part.CFrame = CFrame.lookAt(part.Position, part.Position + hitNormal)
2 Likes

Here is another method, using CFrame rotation between two vectors in order to align a parts UpVector and the surface normal vector of the wall.

You want to look at the RaycastResult.Normal.

https://developer.roblox.com/en-us/api-reference/datatype/RaycastResult

It’s a unit vector that points away from whatever surface you hit.

Here’s a nice image visualizing normals.

image

You can use this normal vector and have the part rotate in the same direction. You can also invert it if you want the part to look the other way by just doing -normal.

1 Like

Oh, I may have overlooked the problem, thank you! I feel stupid into thinking it was such a simple solution, haha…