Roblox Viewmodel Collision Prevention :Lerp() position bug Help

  1. I am trying to get rotational offset and position offset on my viewmodel to transition smoothly and react fast without the position bug.

Examples:
Shown at: 0:26 / 0:50

  1. What I currently have. In both videos listed, there’s a conflict happening between the two :Lerp()'s
--Settings/configs
local CollisionUpdate = .1
local MaxCollisionOffset = Vector3.new(0,0,-.5)
local MaxCollisionOffsetRotation = Vector3.new(0,0,-.45)
local CollisionOffset = Vector3.new(0,0,0)
local ExtraCollisionOffset = Vector3.new(0,0,0)

local function canSee(from,to,ignore)
	local intIgnore = {}
	for i,v in pairs(ignore) do
		intIgnore[i] = v
	end
	local ray = Ray.new(from,to-from)
	local blocked = false
	local finished = false
	local count = 0
	repeat
		count += 1
		local f = workspace:FindPartOnRayWithIgnoreList(ray,intIgnore)
		if f then
			if f.Transparency > 0 or (f.Parent.Parent and f.Parent.Parent:FindFirstChild('Humanoid')) then
				intIgnore[#intIgnore+1] = f
			else
				finished = true
				blocked = true
			end
		else
			finished = true
		end
	until finished or count > 100
	local Param = RaycastParams.new()
	Param.FilterType = Enum.RaycastFilterType.Exclude
	Param.FilterDescendantsInstances = {intIgnore}
	return workspace:Raycast(from,to-from,Param)
end

--Issue occurs in the :Lerp() areas
stepped_con = game:GetService("RunService").RenderStepped:connect(function(deltaTime)

if tool:FindFirstChild("Handle") and tool:FindFirstChild("Handle"):FindFirstChild("Main") and tool:FindFirstChild("Handle"):FindFirstChild("Main"):FindFirstChild("CollisionEnd") and tool:FindFirstChild("Handle"):FindFirstChild("Main"):FindFirstChild("GunCollisionStart") then
					local Result = canSee(tool:FindFirstChild("Handle"):FindFirstChild("Main"):FindFirstChild("CollisionStart").WorldPosition, tool:FindFirstChild("Handle"):FindFirstChild("Main"):FindFirstChild("CollisionEnd").WorldPosition,{tool,character})
					if Result == nil then
						CollisionOffset = CollisionOffset:Lerp(Vector3.new(),CollisionUpdate)
						ExtraCollisionOffset = ExtraCollisionOffset:Lerp(Vector3.new(),CollisionUpdate)
						if ExtraCollisionOffset ~= Vector3.new() then
							Aimoffset = Aimoffset:Lerp(aimoffset.Value,CollisionUpdate)
						else
							Aimoffset = Aimoffset:Lerp(aimoffset.Value,1)
						end
					else
						CollisionOffset = CollisionOffset:Lerp(Vector3.new(CollisionOffset.X,CollisionOffset.Y,math.clamp(-Result.Distance,MaxCollisionOffset.Z,0)),CollisionUpdate)
						if CollisionOffset.Z <= MaxCollisionOffsetRotation.Z then
							ExtraCollisionOffset = ExtraCollisionOffset:Lerp(Vector3.new(CollisionOffset.X,CollisionOffset.Y,math.clamp(-Result.Distance,MaxCollisionOffset.Z*2,CollisionOffset.Z)),CollisionUpdate) 
							Aimoffset = Aimoffset:Lerp(Vector3.new(),CollisionUpdate)
						end
					end
					
				end
end)

You also need to change the attachment distance for the muzzle, You need to make the front one go forward a few studs more to prevent it from constantly hitting and missing the raycast
I think I found a solution:

if tool:FindFirstChild("Handle") and tool:FindFirstChild("Handle"):FindFirstChild("Main") and tool:FindFirstChild("Handle"):FindFirstChild("Main"):FindFirstChild("CollisionEnd") and tool:FindFirstChild("Handle"):FindFirstChild("Main"):FindFirstChild("CollisionStart") and tool:FindFirstChild("Handle"):FindFirstChild("Main"):FindFirstChild("GunFirePoint") and tool:FindFirstChild("Handle"):FindFirstChild("Main"):FindFirstChild("GunFirePoint0") then
					local Result = canSee(tool:FindFirstChild("Handle"):FindFirstChild("Main"):FindFirstChild("CollisionStart").WorldPosition, tool:FindFirstChild("Handle"):FindFirstChild("Main"):FindFirstChild("CollisionEnd").WorldPosition,{tool,character})
					local DistFix = (tool:FindFirstChild("Handle"):FindFirstChild("Main"):FindFirstChild("CollisionEnd").WorldPosition-tool:FindFirstChild("Handle"):FindFirstChild("Main"):FindFirstChild("GunFirePoint").WorldPosition).Magnitude
					local DistFix2 = (tool:FindFirstChild("Handle"):FindFirstChild("Main"):FindFirstChild("CollisionEnd").WorldPosition-tool:FindFirstChild("Handle"):FindFirstChild("Main"):FindFirstChild("GunFirePoint0").WorldPosition).Magnitude
					if Result == nil then
						CollisionOffset = CollisionOffset:Lerp(Vector3.new(0,0,math.clamp(CollisionOffset.Z+.1,MaxCollisionOffset.Z,0)),CollisionUpdate)
						ExtraCollisionOffset = ExtraCollisionOffset:Lerp(Vector3.new(0,0,math.clamp(ExtraCollisionOffset.Z+.1,MaxCollisionOffsetRotation.Z,0)),CollisionUpdate)
						if ExtraCollisionOffset ~= Vector3.new() then
							Aimoffset = Aimoffset:Lerp(aimoffset.Value,CollisionUpdate)
						else
							Aimoffset = Aimoffset:Lerp(aimoffset.Value,1)
						end
					elseif Result ~= nil and DistFix ~= nil then
						local Fix = (Result.Distance)-(DistFix+DistFix2)
						CollisionOffset = CollisionOffset:Lerp(Vector3.new(CollisionOffset.X,CollisionOffset.Y,math.clamp(Fix,MaxCollisionOffset.Z,0)),CollisionUpdate)
						if CollisionOffset.Z <= MaxCollisionOffsetRotation.Z then
							ExtraCollisionOffset = ExtraCollisionOffset:Lerp(Vector3.new(CollisionOffset.X,CollisionOffset.Y,math.clamp(Fix,MaxCollisionOffset.Z*2,CollisionOffset.Z)),CollisionUpdate) 
							Aimoffset = Aimoffset:Lerp(Vector3.new(),CollisionUpdate)
						end
					end
					
				end

Thanks, I have tested the code you sent me, I think it works.
Is it supposed to do this?

Example Video:

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