How do I prevent gun model going through wall?

Recently I’ve been doing my own gun framework and i wanted to add feature where the weapon wouldn’t go through walls, but would repel from them

I’ve seen this feature in a lot of shooter games, here’s example from “The Eastern War”:


I tried to recreate this feature in my project, and i think I’m almost there, but I really don’t know how to work with positions and CFrames, so it doesn’t work as i intended, and i don’t know what to do.


Basically, I’m just raycasting from character’s head, and if there is some obstacle, then we subtract the position of the player’s arm from the position of the wall, and we add this difference to the Z coordinate of the shoulder’s Motor6D.

I lack knowledge in how the positions and CFrames work, so I would appreciate if someone tells me the correct way how this feature is should be made.

Try disabling collisions for the arm with a CollisionGroup for example ?

1 Like

nah doesn’t work

charscharscharschars

every time the humanoid state changes, the limbs change collision

If you want to achieve this effect you could do.

local ray = workspace:RayCast(Camera.CFrame.Position, Camera.CFrame.LookVector*5.5) -- remember to filter out stuff with raycast params
local Pushback = CFrame.new(0,0,ray.Distance-2) -- Subtract distance from gun length (you can tweak the gunlength value here to reach desired effect) if its reversed just absolute the number via math.abs() or invert it with (-), for smoother effect you could lerp the CFrame

This is what we use in The Eastern War. If you have a question u can dm me trough the MPS comms server (4ce)

1 Like

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