Raycasting Broken

i have made my gun using RayCast but for some reason, as seen in this Video
it ends up just breaking and working fine every so often.
here is my game file if you want to have a look Test.rbxl

The RayCast is shown by the red beam. (sorry for bad animations btw, i am a scripter not an animator)

I have asked ScriptersCF on discord if they know anything but none of us have come up with a solution.

note: i have never really worked with raycasts before and i thought an fps project would be fun, which it is.


The actual problem isn’t with the raycasting, but with your viewmodel.

Your issue is caused by the fact that your velocity is increasing to approximately -8000 Y for every part of your gun. This is because your gun is unanchored, and can’t be noticed since you set your gun’s position directly prior to rendering, however the position will still continuously change while the velocity is so high. Whenever it wouldn’t work for you, that meant the HitFrom part went under the baseplate from the high velocity.

Anchoring the entire viewmodel should fix the issue without affecting behavior.


did that and, well…

1 Like

Ah, I forgot about the animations since I wasn’t able to test them. What might work is anchoring all parts that do not have a Motor6D for them to the Gun’s Main, then welding HumanoidRootPart to an anchored part that you position infront of the camera.

1 Like

all parts have a motor6D for them exept HRP. how would i counteract that?

1 Like

Devforum_Viewmodel.rbxm (44.1 KB)

I updated the viewmodel onto this file.
I added welds in this file, and created a new part and set it as the PrimaryPart. This new part is Anchored and welded to HumanoidRootPart. This should work with animations, however I’m unable to test that.

1 Like

works with animations but jesus christ does it fling you

1 Like

Alright, that’s probably a collision problem. The easiest solution would probably be Collision Groups. You can’t make collision groups or configure them on the client , however you can set them on the client. You’ll want to make a new collision group on the server called “Viewmodel” and have it unable to collide with the Default collision group. You’ll need to loop through your viewmodel on the client, and set every part’s collision group (via phys:SetPartCollisionGroup(Part, "Viewmodel"))

local phys = game:GetService('PhysicsService'); 
phys:CollisionGroupSetCollidable("Viewmodel", "Default", false)

oof studio crashed RIP. sad times

after i finish this gun i have to start on the anti-cheat for aimbot, inf ammo etc

no idea how to make an anti-aimbot

still getting yeeted whenever i zoom in. what part should i put the Viewmodel collision group to? HRP or whatever?

All parts of the viewmodel. Especially the left and right arms.

ok, should i do that with a for loop?

That’d probably be the better way to do it, yes.

this turns out to be a problem with many viewmodels who get their ray origin from the actual viewmodel, thank you for contributing