I am making a gun for a game, I want to raycast from the gun so you can't shoot through walls. I do that by first raycasting from the camera, then raycasting from the gun to the position that got hit. The problem is that it seems to work randomly: I clicked in the same spot and got different results every time. (The first output is the second raycast and the second one is the camera raycast. Sorry that it is confusing)
#Raycasting problem
6 messages · Page 1 of 1 (latest)
You want it to shoot through walls?
That isn't what you're doing at all you're just casting rays from two different places. To make it shoot through the wall cast a ray from the raydestination with the same ray direction
He doesnt want to shoot through walls
Oh it says can't shoot through walls I can't read
Okay then your problem is the ray is exactly the distance to where your mouse hit and your gun and not beyond it. So just extend the ray a bit. Really you should do this:
local rayDirection = (rayDestination - tool.Handle.ShootPart.Position).Unit
local finalRay = workspace:Raycast(tool.Handle.ShootPart.Position, rayDirection * 1000, raycastPrams)