#Raycast not hitting anything when at certain angles

1 messages · Page 1 of 1 (latest)

loud raft
#

I have a basic gun script. I'm using raycast to make a bullet and have the params ignore my character. For some reason though, the gun only fires have the time when it is at certain angles and I don't know why. (Note: it is a server issue because I tested and the client detects the input and fires the event)

function for the gun shooting:

fired.OnServerEvent:Connect(function(plr, target : Instance, pos : Vector3)
    local params = RaycastParams.new()
    params.FilterDescendantsInstances = {plr.Character}
    params.FilterType = Enum.RaycastFilterType.Exclude
    params.IgnoreWater = true
    
    if deb == false and reloading == false then
        if bulletsLeft > 0 then
            if target then
                local result = workspace:Raycast(tool.Shoot.Position, pos.Position - tool.Shoot.Position, params)
                deb = true

                if result then
                    rayVis(tool.Shoot.Position, result.Position)

                    if result.Instance then
                        if result.Instance.Parent:FindFirstChild("Humanoid") then
                            local hum = result.Instance.Parent:FindFirstChild("Humanoid")

                            hum:TakeDamage(10)
                        end
                    end
                    
                    shotSound:Play()
                end
                
                bulletsLeft -= 1
                task.wait(SHOT_DELAY)
                deb = false
            end
        end
    end
end)
iron granite
#

@loud raft

#

ik this is ai but

#

Its bcs

#

U made it so it only shoots

#

When

#

Incontact with humnoid

loud raft
#

That’s not the issue because the visualiser for the gun should appear when there is a result

#

It says if result then rayvis()

#

Rayvis() is a visualiser function

#

For those who see this post I figured it out

#

The pos variable was a CFrame

#

so i had to modify the raycast direction to this

#

local direction = (pos.Position - origin).Unit * 1000

#

since CFrame.position gives me the world pos not the direction or something like that

dark agate
#

You're only doing the full sequence if there is a target, i would use fast cast, its much easier to use. I will send an example gun system

#

You really only need to pay attention the the last 2 functions the onserverevent and function GetCasterForGun

#

The clientside is incredibly simple, this is just a testing script that I was using until I made the actual guns

--[[
Not_Lowest
Temp gun script
]]

local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()
local Event = ReplicatedStorage.Remotes:WaitForChild("GunEvent")

local function Shoot(Gun)
    local Barrel = Gun:FindFirstChild("BPoint")
    if not Barrel then return end

    local Origin = Barrel.Position
    local Direction = (Mouse.Hit.Position - Origin).Unit * 1000 
    local Params = RaycastParams.new()
    Params.FilterDescendantsInstances = {Player.Character, Gun}
    Params.FilterType = Enum.RaycastFilterType.Exclude

    local Result = workspace:Raycast(Origin, Direction, Params)

    Event:FireServer("fire", Result and Result.Instance, {Position = Mouse.Hit.Position})
end

UserInputService.InputBegan:Connect(function(input, gp)
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
        local Gun = Player.Character and Player.Character:FindFirstChildWhichIsA("Tool")
        if Gun then
            Shoot(Gun)
        end
    end
end)
tame flintBOT
#

studio** You are now Level 1! **studio

dark agate
#

Im not entirely sure if this will work because im not in studio but

fired.OnServerEvent:Connect(function(plr, target: Instance, pos: Vector3)
    local params = RaycastParams.new()
    params.FilterDescendantsInstances = { plr.Character }
    params.FilterType = Enum.RaycastFilterType.Exclude
    params.IgnoreWater = true

    if deb or reloading then
        return
    end

    if bulletsLeft <= 0 then
        return
    end

    if not target then
        return
    end

    deb = true

    local origin = tool.Shoot.Position
    local direction = (pos.Position - origin).Unit * 1000
    local result = workspace:Raycast(origin, direction, params)

    local hitPosition = result and result.Position or (origin + direction)
    rayVis(origin, hitPosition)

    if result and result.Instance then
        local parent = result.Instance.Parent
        local hum = parent:FindFirstChild("Humanoid")
        if hum then
            hum:TakeDamage(10)
        end
    end

    shotSound:Play()
    bulletsLeft -= 1

    task.wait(SHOT_DELAY)
    deb = false
end)