How to Raycast with detecting Parts in the Way

Hey there Other Devs,

Recently I started a new project trying to create a gun, but I quickly ran into a problem where my raycast to the mouse would phase through walls.

Here is a video clip of the bug:


You can see the bullets phase through the wall, ignoring it.


The code:

-----// Raycasting

local GunParams = RaycastParams.new()
GunParams.FilterType = Enum.RaycastFilterType.Exclude
GunParams.FilterDescendantsInstances = {Character}

local function MouseRaycast(Params)
	
	local MousePos = UserInput:GetMouseLocation()
	local MouseRay = Camera:ViewportPointToRay(MousePos.X, MousePos.Y)
	local result = workspace:Raycast(MouseRay.Origin, MouseRay.Direction * 100, Params)
	
	if result then
		return result
	end
	
end

Thanks for the help!

You need to ensure that the RaycastParams are set up correctly and that your raycasting logic is accurately detecting the parts in the way.

This method converts the 2D mouse position into a 3D ray that originates from the camera. Make sure you are using this method correctly to get the ray from the mouse position. You should also make sure that the filter type is set to Exclude and that the character is excluded from the raycast to prevent self-hits.

local Players = game:GetService("Players")
local UserInput = game:GetService("UserInputService")
local Camera = workspace.CurrentCamera
local Character = Players.LocalPlayer.Character or Players.LocalPlayer.CharacterAdded:Wait()

-- Set up Raycast Parameters
local GunParams = RaycastParams.new()
GunParams.FilterType = Enum.RaycastFilterType.Exclude
GunParams.FilterDescendantsInstances = {Character}

-- Function to perform the raycast
local function MouseRaycast(Params)
    local MousePos = UserInput:GetMouseLocation()
    local MouseRay = Camera:ViewportPointToRay(MousePos.X, MousePos.Y, 0)
    local RayDirection = MouseRay.Direction * 100 -- You can adjust the length as needed
    local result = workspace:Raycast(MouseRay.Origin, RayDirection, Params)

    if result then
        return result
    end
end

-- Example usage of the MouseRaycast function
local result = MouseRaycast(GunParams)
if result then
    print("Hit: ", result.Instance:GetFullName())
else
    print("No hit detected")
end
2 Likes

hi there, thanks for thje help but the ray still doesnt pick up the wall in the way.

so i cant rewrite the code but currently your raycast and bullet trajectory scripts are doing two different things.

your raycast is getting a position from the screen based on where you click. the bullet trajectory is taking whatever position given and creating a bullet and its trail.

the issue is, you need another raycast from the tip of your gun to the mouse position given from the raycast. if there’s a hit, then you need to update the mouse position to the new one.

also, you should make a check to make sure the player isnt shooting behind them. or on their side. it should only accept mouse positions if they are actually infront of the player.

Couple things to double check:

  • Ensure the wall is not in the FilterDescendantsInstances: Double-check that the wall or any other parts you want to detect are not being excluded by the raycast parameters.
  • Check collision settings: Ensure that the wall has the correct collision properties and is not set to be non-collidable.