How to understand Raycasts?

I’ve been trying to understand raycasts for 1/2 hours and still could not understand them
I still can’t figure out how to make a raycast that actually works

This is the code that doesn’t work:

local part = workspace:FindFirstChild("RaycastPart")

local function FireRaycast()
	
	local rayOrigin = part.Position
	local rayDirection = Vector3.new(100,0,0)
	
	local raycastResult = workspace:Raycast(rayOrigin, rayDirection)
	
	if raycastResult then
		local hit = raycastResult.Instance
		
		print(hit)
	end
	
end

FireRaycast()

The script works, but what do you want to do?

RayOrigin is where the ray will start, RayDirection is where the ray will go

ray.instance is anything that the ray hit, heres a quick drawing that i made to hopefully make this easier to understand lol

Oh wait it actually works

It’s probably becouse i only fired it once in the script and it hit nothing

Thanks! This made it a bit more clear

I still don’t understand why the rayDirection is a position and not an orientation

Also is there a way to have the ray allways shoot from the parts front surface?

yea, you can have the raydirection be the parts lookvector

example code:

local part = workspace.raypart

while wait() do
	local ray = workspace:Raycast(part.Position, part.CFrame.LookVector*5)
	
	if ray then
		local inst = ray.Instance
		print(inst.Name)
	end
end
1 Like