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

Raycast not shooting the correct direction?

im vary new to using raycasts as im trying to make it so the bots cant see players through walls. sight problem though. it wont shoot in the correct direction.

heres the script

while true do
	if script.Parent.Target.Value ~= nil then -- making sure its targeting someone
		local TargetSpot = script.Parent.Target.TargetRootPoss.Value -- determons where to shoot (im 100% sure this is working fine)
		local RayParams = RaycastParams.new()
		RayParams.FilterDescendantsInstances = {script.Parent.Parent}
		RayParams.FilterType = Enum.RaycastFilterType.Blacklist
			wait(.1)
		local RayResult = game.Workspace:Raycast(script.Parent.HumanoidRootPart.Position, TargetSpot, RayParams)
			wait(.1)
	if RayResult ~= nil then
			print(RayResult.Instance.Parent.Name.." got hit by the funni raycast get nae naed. ("..(RayResult.Position - script.Parent.HumanoidRootPart.Position).Magnitude.." studs away)")
		elseif RayResult == nil then
			print("Ray hit nothing.")
		end
	end
	wait(0.1)
end

i dont care if you have a better way to optimize it more. if it doesn't fix this problem please dont comment it because i like to have the scripts mostly done by myself. i just cant find a fix for this problem.

ight ima go to bed see ya tomorrow, thanks in advance!

1 comment

0

this can be cuz second parameter is direction, not target position, to get the direction you do TargetSpot - script.Parent.HumanoidRootPart

1 answer

The second parameter is the direction of where you want the raycast to go, NOT the position.

If you want to get the direction of the TargetSpot you would subtract TargetRootPoss.Value by the origin.

EX:

while true do
 	if script.Parent.Target.Value ~= nil then -- making sure its targeting someone
	        local TargetSpot = script.Parent.Target.TargetRootPoss.Value - script.Parent.HumanoidRootPart.Position -- There you go!
	        local RayParams = RaycastParams.new()
	        RayParams.FilterDescendantsInstances = {script.Parent.Parent}
	        RayParams.FilterType = Enum.RaycastFilterType.Blacklist
	            wait(.1)
	        local RayResult = game.Workspace:Raycast(script.Parent.HumanoidRootPart.Position, TargetSpot, RayParams)
	            wait(.1)
	    if RayResult ~= nil then
	            print(RayResult.Instance.Parent.Name.." got hit by the funni raycast get nae naed. ("..(RayResult.Position - script.Parent.HumanoidRootPart.Position).Magnitude.." studs away)")
	        elseif RayResult == nil then
	            print("Ray hit nothing.")
	        end
	    end
	    wait(0.1)
	end

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