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

[SOLVED]Why is the ray ignoring body parts?[SOLVED]

Asked by
Mineloxer 187
6 years ago
Edited 6 years ago

My gun uses ray casting for the detection system. If it detects a possible descendant of a player's character, it fire a remote event to be handled by the client. This works fine for the torso, head and arms, but when I try shooting on the legs, it completely ignores it and goes though it.

Shoot function in the Local Script:

local function shoot()  
    if not aiming then return end

    --//Initialziation
    shot:FireServer()

    --//Ray
    local ray = Ray.new(hole.CFrame.p, (mouse.Hit.p - hole.CFrame.p) * range.Value)
    local hit, position = workspace:FindPartOnRay(ray, player.Character, false, true)

    --//Hit Detection
    if hit then
        print(hit.Name) --I added this here to debug, which is how I found at that it passes through the body parts.
        --//Check humanoid
        local humanoid = hit.Parent:FindFirstChild("Humanoid")
        if humanoid then
            --//Get target player
            local hitPlr = players:GetPlayerFromCharacter(hit.Parent)
            if hitPlr then
                --//Hit a possible target
                hitRemote:FireServer(hitPlr.Name, hit.Name)
            end
        end
    end
end

Something I feel is really important to mention is that, I tried shooting at a spawn location, and I noticed it went through and printed the base plate instead. This happened when I tried shooting on the sides, but not the top.

Any help is appreciated.

2 answers

Log in to vote
2
Answered by 6 years ago

I don't know exactly how your ray gun is supposed to work, but try something like:

(untested pseudocode based on the iconic laser gun)

local mouse = game.Players.LocalPlayer:GetMouse();

-- I assume this is a tool, and a localscript exists as a child of the tool
script.Parent.Activated:connect(function()
    if(not aiming)then
        return true;
    end
    shot:FireServer();
    local target = mouse.Target;
    if(target ~= nil)then
        if(target.Parent:FindFirstChild("Humanoid"))then
            playerhit:FireServer(target.Parent.Name, target);
        end
    end
end)

The basic premise is that Roblox already knows what part your mouse is over. We also don't need to ignore the LocalPlayer, because playerhit (the remoteevent) receives the player's name as it's first parameter: therefore, for playerhit(player, name, part), if player is equal to name than nothing should happen.

To create your ray, create a new part between the barrel of the gun and mouse.Hit:

Something along the lines of

local part = Instance.new("Part");
part.Parent = game.Workspace;
part.Anchored = true;
part.Size = (distance between points, y, z)
-- new cframe at position gun.barrel looking toward mouse.Hit
part.CFrame = CFrame.new(gun.barrel, mouse.Hit);
-- offset cframe by 1/2 the part's size
part.CFrame = part.CFrame * CFrame.new(x, y, z);

I'm tired though, so you'll have to figure out exactly how to implement this yourself. Enjoy.

0
Thank you for the detailed answer, I feel bad it for you. It was only an issue with the math. Thanks for the answer though! Mineloxer 187 — 6y
Ad
Log in to vote
0
Answered by
Mineloxer 187
6 years ago

If anyone had the same issue as me and happen to find this, I realized the issue was with the math, when I casted the ray. Always check the math from a credible source like the wiki :)

0
gg KidTech101 376 — 6y

Answer this question