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.
What is the issue? Include screenshots / videos if possible!
They phase through the walls to reach where the mouse pointed.
The rest of this item relies on raycasts, I need this fixed.
local raycastResult = workspace:Raycast(mouse.UnitRay.Origin, mouse.UnitRay.Direction * 2500,ignoreList)
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.
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.
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.
Using an attatchment in the tool, from WorldPosition:
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
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.
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?
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
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.