Raycasts go to the mouse - piercing walls

  1. What do you want to achieve? Keep it simple and clear!
    I want my raycasts to stop when they hit a Part, either that being a character’s parts or the world’s parts.

  2. What is the issue? Include screenshots / videos if possible!
    They phase through the walls to reach where the mouse pointed.
    image
    The rest of this item relies on raycasts, I need this fixed.

local raycastResult = workspace:Raycast(mouse.UnitRay.Origin, mouse.UnitRay.Direction * 2500,ignoreList)
  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried changing what is excluded from my raycast and that instead hits the player when they try to shoot.
2 Likes

I found a similar topic that might answer your question:

1 Like

I am having issues understanding this in the post you linked. Where do I get destination in this?

Change this to the humanoid root part’s position and blacklist the player’s character.

1 Like

image
The same issue remains from before. I am not sure how this is different from the other method I used.
I’ll try again using the Head instead

1 Like

When not in first person, the resulting shot is off-course from where you are aiming. @Kaid3n22 your solution gives up accuracy for not piercing through walls.

The stem of the issue was that: in third person you could shoot through walls.
Making third person inaccurate would not be helpful in this regard.

1 Like

Seems like mouse.UnitRay uses the camera’s viewport position to the mouse’s 3D position. You could try using a different reference point for your directional vector, perhaps the character’s head or the tool’s muzzle.

2 Likes

Using an attatchment in the tool, from WorldPosition:
image

Most of these solutions make accuracy a problem, which is quite concering considering that it should be as accurate as possible and not go through walls

1 Like

Sorry, I didn’t read the entire code and figured the origin would be the only issue. You should use the position of the firing part of the gun or the position of the HumanoidRootPart as the origin of the ray, and you should use mouse.Hit.p as the end of the ray (Also sorry for the late reply, I wasn’t home up until now). I believe your current mouse.UnitRay.Direction measures the direction of the mouse from the camera and not the player, but with mouse.Hit.p, it gets the position in the word that the mouse hits, meaning it would work no matter what as an endpoint.

1 Like

I now have a new issue related to this.
The raycast fires backwards.

local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.FilterDescendantsInstances = {plr.Character,tool}
raycastParams.IgnoreWater = true
local rayOrigin = plr.Character:FindFirstChild("Head")
local range = 2500

local function FireCast() --Unrelated code comment
	local direction = (rayOrigin.Position - mouse.Hit.Position) --Fires in a seemingly random direction (????)
	local raycastResult = workspace:Raycast(rayOrigin.Position, direction * range,raycastParams)
	return raycastResult
end

Thank you for your help.
Should I just reverse the direction of mouse.Hit.Position?

1 Like

if that’s the case, do -range instead of range

1 Like

In this case, I think changing this to a unit will work. (if it still fires backward, I would do -range as @NGC4637 said or flip the ray origin and mouse.hit when subtracting.

local direction = (rayOrigin.Position - mouse.Hit.Position).Unit
1 Like

The raycasts not working was a problem of stupidity. I flipped the equation and it works perfectly. It now fires where the mouse cursor is, and not where it wants to or the coordinates in the world of the part’s origin.

They now work perfectly :slight_smile:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.