Viewmodel wall avoidance help

i’m currently making a gun system that is straying from the more arcadey type and leaning over to the more realistic side like how escape from tarkov or insurgency:sandstorm does it, but i’m attempting to code a functionality where if the viewmodel gets too close/impacts a wall it will move back away from the wall and not clip through it but i’m extremely confused on how to go about doing it.

my current code partially works in my favor, but upon impact it keeps going back then immediately stops the raycast which then triggers it to return back and vice versa

vm:SetPrimaryPartCFrame(game.Workspace.CurrentCamera.CFrame * require(vm.Offset) * moveoffset)
game.Workspace.CurrentCamera.CFrame = game.Workspace.CurrentCamera.CFrame

local result = game.Workspace:Raycast(vm:WaitForChild(tool.Name):WaitForChild("Barrel").Position,vm:WaitForChild(tool.Name):WaitForChild("Barrel").CFrame.LookVector,viewmodelParams)
			
			if result then
				
				moveoffset = moveoffset:Lerp(moveoffset + Vector3.new(0,0,0.3),0.5)
				
				else
				
				moveoffset = moveoffset:Lerp(CFrame.new(0,0,0),0.5)
				
			end

824e3ffe7af9aa52d4d645f8adaa1c53

5 Likes

you should make a part at the end of the gun then use that

also do not add offset in my implementation i added the position of the moveoffset to the direction of the ray

forgot to close this post haha

i’ve done something similar to that when, well, i was actually still trying to finish the framework. i’ve ended up putting an attachment that is automatically placed roughly around the end of the gun and some other hoobajeebie to stop the bugginess.

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