Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to make a BodyPosition stay at a certain slanted height using Raycasting?

I have a brick with a script that tells a ray to shoot downwards from the rotation of the brick. Once the ray hits an object I grab that position and add 10 to the height. The problem with that is that the height is according to the global XYZ as seen here: https://gyazo.com/2023e0f4b18f94076a5036cf730b15cc

t = the bodyposition object

ray = Ray.new(start, (down))
				object, hit = workspace:FindPartOnRay(ray)
				if object then
					t.Position = hit + Vector3.new(0,10,0)

What line of code would I add to offset 10 from the hit positions rotation? Like this: https://gyazo.com/d39d04e8de680db263e1a05ccae3d6dd

1 answer

✓ Accepted answer

Luckily for you, roblox somewhat recently added another return to the FindPartOnRay method - the normal vector. It basically shows the direction of hit surface, so you can nicely use it in your situation.

ray = Ray.new(start, (down))
object, hit, normal = workspace:FindPartOnRay(ray)

if object then
	-- The normal shows you the direction of surface, so you just set it to whatever distance you need
	t.Position = hit + normal * 10
end

2 comments

0

Does this work on smooth terrain as well?

0

It should.

This discussion is preserved as it was. New answers, comments, and votes are closed.