Get orientation from a Raycast hit

I want to create a gun that makes a surface at the position of a raycast hit. The position works, however, I also want to orient the part so it’s equal with the hit’s surface.

This is how the gun works right now.

This is how the part is currently being oriented:
image

This is how I want the part to be oriented:

This is my code:

I’ve searched the forum but I didn’t really find an answer. Would anyone help?

3 Likes

The raycast normal has the info needed to align with the surface orientation.

This link should be great for just this :slight_smile:
https://developer.roblox.com/en-us/api-reference/function/WorldRoot/Raycast

image

1 Like

Use raycast normal to achieve that and set the cframe of a part instead of a position. Try something like this:

p.CFrame = CFrame.new(Pos, Pos + Normal)

You may have to multiply this by some constant depending on your part

5 Likes

Thanks, I actually found a post about this somewhere, it has the same solution as you.

1 Like