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

Raycast not shooting the correct direction?

Asked by 2 years ago

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!

0
this can be cuz second parameter is direction, not target position, to get the direction you do TargetSpot - script.Parent.HumanoidRootPart imKirda 4491 — 2y

1 answer

Log in to vote
0
Answered by 2 years ago

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

Ad

Answer this question