Unexpected Behavior Trying to Prevent Viewmodel From Wall Clipping

I’m trying to make my viewmodel push back whenever it gets close to a wall, depending on how close it is.

But it’s inverted, basically.

Not close, code is acting as if I was at the closest point, in reality I’m at the farthest point

Very close, code is acting as if I was at the farthest point, in reality I’m at the closest point

Code:

			local Origin = Camera.CFrame.Position
			-- fp2c is the distance from the muzzle to the camera, used for raycasting
			local Direction = Camera.CFrame.LookVector*(Tool.FP2C.Value)
			
			local Cast = game.Workspace:Raycast(Origin, Direction, Params)
			
			if Cast then
								
				local Distance = Cast.Distance
				print(Distance)
				
				script.Offset0.Value = CFrame.new(0, 0, Distance) -- t
			else
				script.Offset0.Value = CFrame.new(0, 0, 0)
			end

If I set distance to negative, it does this

far

close

so it does get closer, but it starts off way farther than it’s supposed to be.

I did:

distance = cast.distance-(camera distance from tool muzzle)

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