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

[SOLVED] Why does my Raycast shoot from out the ground instead of the dedicated FirePart?

Asked by 3 years ago
Edited 3 years ago

I am making an FPS game. I've made the guns but there is one problem; Every time I shoot a little bit too low, it will either shoot straight from the ground, or just shoot from the ground into the ground instead of the FirePart (Which is an Attachment in a part ) inside of the gun. Here's a video showing the problem (Please read description and have subtitles on): https://youtu.be/os99udWOqEI

Here is the where the shoot event gets fired from a local script:

raycastEvent:FireServer(mouse.Hit.Position, weapon:WaitForChild(weapon.Name):WaitForChild("FirePart"):WaitForChild("FireAttachment").WorldPosition, weapon:GetAttribute("Damage"), false)

Here is the scripts that receives the event when it gets fired (I shortened it down a bit for this question):

local raycastEvent = game.ReplicatedStorage:WaitForChild("RemoteEvents"):WaitForChild("RaycastEvent")


raycastEvent.OnServerEvent:Connect(function(player, endPos, startPos, damage, aimMode)
    local params = RaycastParams.new()
    params.FilterType = Enum.RaycastFilterType.Blacklist
    params.FilterDescendantsInstances = {player.Character}
    local raycastResults

    raycastResults = workspace:Raycast(startPos, (endPos - startPos).unit * 100, params)

    local character = player.Character or player.CharacterAdded:Wait()

    local head = false

    if raycastResults then
        local part = raycastResults.Instance
        local pos = raycastResults.Position
        local humanoid = part.Parent:FindFirstChild("Humanoid")

        print(part.Name)

        if humanoid then
            if part.Name == "Head" then
                head = true
                humanoid:TakeDamage(damage*2)
            else
                head = false
                humanoid:TakeDamage(damage)
            end
        end
    end
end)

If you need anymore information please ask me. Thanks for reading!

1
You should probably start the ray from the character's head position and use CurrentCamera.CFrame.LookVector. That's how I got mine to work. Neatwyy 123 — 3y
0
Alright, thanks! ColdFoxy07 76 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

Thanks to Neatwyy, I fixed it! I just made the raycast start from the HumanoidRootPart and it worked flawlessly

Ad

Answer this question