Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Need help in Ray Castings?

Asked by 8 years ago

What is the most effective way of Ray Casting? Do any of you have any tip on Ray Casting?

0
using CFrame. look Vector. scottmike0 40 — 8y
0
game.Workspace:FindPartOnRay()... Or something like that lightpower26 399 — 8y

1 answer

Log in to vote
2
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
8 years ago

You should definitely use Roblox's raycasting system. According to the wiki, it's 105 times faster than the most efficient known user-made raycasting algorithm. This is because Roblox could code it in C++.

You use Ray.new to create a new ray. You will then supply the origin and the direction of the ray as arguments.

local ray = Ray.new( Vector3.new(0, 0, 0,), Vector3.new(0, 50, 0) ) --Makes a ray shooting upwards from 0,0,0 for 50 studs.

You can then use the FindPartOnRay method of workspace to see if your ray hit something. FindPartOnRay returns a tuple, including the part that the ray hit and the position that it hit at. If it didn't hit anything, hit and position will be nil and the end of the ray, respectively.

hit, position = workspace:FindPartOnRay(ray)
print( hit, position )
Ad

Answer this question