Need help with rayCasting

Hello! I was trying to make my first gun but then I got stuck on the raycasting part. The gun seems to work decent but something the raycast it’s not how it should be. Even if I have them mouse on top a character, the raycasting goes besides it, and it doesn’t touch it. Anyone who could help me?

This is the code of the raycast part.

HitPositon = Mouse.Hit
ShootPart = the on the gun where the bullet comes out

local function rayCasting(HitPosition)

local currentRay = Ray.new(ShootPart.Position, HitPosition.LookVector * 100)

local HitPart, HitPartPosition = game.Workspace:FindPartOnRay(currentRay)

return HitPart, HitPartPosition, currentRay

end

This is an image of house the raycast goes besides the character, into the void…

image

Move the HitPosition variable inside of the function. It is only getting defined once so it will have a position that does not change, moving inside the function will update each time your raycast.

It is updated every time. I just puted it up there just so u know the meaning of it…

Try using the line below

local currentRay = Ray.new(ShootPart.Position, (HitPosition-ShootPart.Position).unit * 100)
1 Like

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