I’m trying to make a gun that raycasts in the direction where the mouse is, but it’s going completely nuts and not hitting the body parts where the mouse is
local ray_params = RaycastParams.new()
ray_params.FilterType = Enum.RaycastFilterType.Exclude
ray_params.FilterDescendantsInstances = {p_char}
usr_input_svc.InputBegan:Connect(function(input, game_processed)
if equipped and not game_processed then
if input.UserInputType == Enum.UserInputType.MouseButton1 and not cd then
cd = true
local ray_result = workspace:Raycast(HOLE.CFrame.LookVector, p_mouse.Hit.Position, ray_params)
if ray_result then
local ray_instance = ray_result.Instance
if ray_instance.Parent:FindFirstChildWhichIsA("Humanoid") then
local target = ray_instance.Parent:FindFirstChildWhichIsA("Humanoid")
script.Parent.SHOT:FireServer(ray_instance, target)
print(ray_instance)
end
end
local shot_anim = p_hum:WaitForChild("Animator"):LoadAnimation(script.Parent.shot_anim)
shot_anim:Play()
script.Parent.HOLE.shot_particle:Emit(1)
script.Parent.shot_sound:Play(0,0,20)
wait(0.1)
cd = false
script.Parent.HOLE.shot_particle:Clear()
end
end
end)
local ray_result = workspace:Raycast(HOLE.CFrame.Position, (p_mouse.Hit.CFrame.LookVector * 100), ray_params)
You can change the 100 to anything It is basically the max length of the ray (Max is 5000 studs)
Also I am not sure what the HOLE variable is but if its a Basepart where the bullet fires from i would say use HOLE.Position instead of HOLE.CFrame.Position
Bro I’m so confused looking at your parameters. For the raycast, you’ll want the POSITION (not lookvector, which is the facing direction), THEN you’ll want the direction, which you can either find by subtracting the origin and destination OR now you’ll wanna use the LookVector*5 to go straight relative to the origin, then ofc the params.
The origin is the gun hole and the direction was supposed to be where the mouse is at… but making it in the center of the screen should work cus it’ll be a first person game
The lookvector is the direction, if it will be first person then you’ll want to make the 2nd parameter (direction), either straight from your camera subtracted with mouse.Hit.Position IF you’re using the camera as the origin which will shoot whever you’re looking. If as you said you want to use the gun barrel or hole as the origin then subtract the origin’s POSITION and mouse’s POSITION.