Raycasting being inaccurate at the starting point

Currently I’m making a shotgun, however there’s a simple raycasting problem that I have no idea why it happens:
https://gyazo.com/85c421635479562d0b186e332f6bc06e

The raycast was casted from an invisible part named ShootPart which is positioned in the front of the barrel, here’s some part of the code:

local ray = Ray.new(ShootPart.CFrame.p, dir * ShotgunConfigs.Range.Value)
...
local distance = (ShootPart.CFrame.Position - pos).magnitude
BulletPart.CFrame = CFrame.new(ShootPart.CFrame.p, pos)

The shotgun spread code is referenced here.

Apart from moving the ShootPart, how do I solve this? I don’t see any code that makes the bullet trail to have such behavior.

Thanks!

Can you please click on the ShootPart when you shoot to show us its position?
Maybe it’s being moved when you shoot

Sorry I can’t show right now (I’m on mobile) but I’m pretty sure it’s position remain constant

Ok, so another prediction is that the ray is starting at the correct position, but the bullets are starting in a wrong position

That’s probably it, is there any solution to this?

You might be spawning them in the wrong position.

Something wrong with this line? Any possible solutions?

i mean your setting it to ShootPart which is front of barrel?

The problem is pretty simple: The shootPart is situated at the end of the barrel. You should move it backwards so the start of the ray does not go through objects.

1 Like

Can you show how big the ShootPart is on your weapon?

When you do ShootPart.CFrame.p, that refers to the center of the part. There’s a chance that the ShootPart’s center is offset from the end of the barrel.

The ShootPart is 0.2,0.2,0.2

This is where the shoot part located:
image

Then that is why the gun is shooting forward that much. You start the ray there, so it can easily clip through walls. Try moving the part back towards the gun.

You should check the visual bullet trail code again. The trails depicted in the video would seem to have the correct focal point from the shootpart, and not intersecting like what being placed way ahead would suggest.

This doesn’t seem to be an issue with raycasts at all, but rather where the bullet is when the trail first renders. Trail.MinLength and Trail.MaxLength may also not have the ideal settings for how you want it to look.

1 Like

Set the CanCollide property to false if you don’t do that already ( for the bullets )
And if that didn’t work then try setting the CFrame like this

BulletStuff.CFrame = CFrame.new(Handle.CFrame * GunData.BarrelOffset.p, position)

not sure if this would work but yeah
( This what I did in my prototype gun system, and Yes I had shotguns and such I’ll post a screenshot of how it looked like, not the best but I was just testing serverside hit detection )
I use that for all my guns and this is the output I always get and is expected :

(If it wasn’t obvious from the video then uh my spread is handled differently and uses something else for spread and accuracy, so yes my answer assumes that it’s the spread system that’s not broken)

Also is it possible for you to show us how what “dir” is?

1 Like

dir can be found here:

Not related to this topic but how do you do the server hit detection?