ADS is not very accurate (CFrame lerping | I am very bad with cframes)

greetings,

it is notsad again, and this time i appear to be struggling with a cframe issue!
i’m trying to make an ADS system by setting the position of an aimpart inside the gun to the cframe of the camera (see code below)

truthfully speaking, i have no idea what i’m doing and i am very bad at this
but the process should look like this:

  • calculate position every frame to account for viewmodel positions changing (etc.)
  • lerp to that position
  • the lerping ‘stops’ when the aimPart reaches the camera

the aimpart is slightly behind the (non-existent) back-sights on the AK47


if this wasn’t obvious, this is NOT what aiming down sights looks like

my code looks like this

Aim = function(gun: Tool, viewmodel: Model)
		local fractured = utility.GetGameplayValue('fractured')
		if fractured.Value then return end
		
		local effectParts: Folder = viewmodel.EffectParts
		local aimPart: BasePart = effectParts.AimPart
		
		local aimSpeedTweak = utility.GetGameplayValue('aimSpeedTweak')
		local speed = 1 / (vanillaAimSpeed / aimSpeedTweak.Value)
		local originalCFrame = viewmodel:GetPivot()
		
		rService:UnbindFromRenderStep('WeldViewmodel')
		rService:BindToRenderStep('Aim', Enum.RenderPriority.Camera.Value, function(deltaTime)
			local aimOffset = aimPart.CFrame:ToObjectSpace(viewmodel.PrimaryPart.CFrame)
			local targetPrimaryCFrame = camera.CFrame * aimOffset:Inverse()

			local alpha = math.clamp(deltaTime * speed, 0, 1)
			local currentCFrame = viewmodel:GetPivot()
			local newCFrame = currentCFrame:Lerp(targetPrimaryCFrame, alpha)

			viewmodel:PivotTo(newCFrame)
		end)
		
		aim:FireServer(true)
		
		mouse.Button2Up:Once(function()
			rService:UnbindFromRenderStep('Aim')
			rService:UnbindFromRenderStep('AimReturn')
			aim:FireServer(false)

			local returnSpeed = 1 / vanillaAimSpeed 
			rService:BindToRenderStep('AimReturn', Enum.RenderPriority.Camera.Value, function(deltaTime)
				local currentCFrame = viewmodel:GetPivot()
				local alpha = math.clamp(deltaTime * returnSpeed, 0, 1)
				local newCFrame = currentCFrame:Lerp(originalCFrame, alpha)

				viewmodel:PivotTo(newCFrame)

				if (newCFrame.Position - originalCFrame.Position).Magnitude < 0.1 then
					viewmodel:PivotTo(originalCFrame)
					rService:UnbindFromRenderStep('AimReturn')
					utility.ReweldViewmodel(viewmodel, camera)
				end
			end)
		end)
	end,

please help :pray: :wilted_flower: :sob:

fixed

Aim = function(gun: Tool, viewmodel: Model)
		local fractured = utility.GetGameplayValue('fractured')
		if fractured.Value then return end
		
		local effectParts: Folder = viewmodel.EffectParts
		local aimPart: BasePart = effectParts.AimPart
		
		local aimSpeedTweak = utility.GetGameplayValue('aimSpeedTweak')
		local speed = 3 / (vanillaAimSpeed / aimSpeedTweak.Value)
		local originalCFrame = viewmodel:GetPivot()
		
		local function GetCameraYRotation()
			local look = camera.CFrame.LookVector
			local flatLook = Vector3.new(look.X, 0, look.Z).Unit
			
			return CFrame.lookAt(camera.CFrame.Position, camera.CFrame.Position + flatLook)
		end
		
		local baseCameraCFrame = GetCameraYRotation()
		local cameraOffset = baseCameraCFrame:ToObjectSpace(viewmodel:GetPivot())
		local aimOffset = viewmodel.PrimaryPart.CFrame:ToObjectSpace(
			aimPart.CFrame * CFrame.Angles(0, math.pi, 0)
		)

		rService:UnbindFromRenderStep('WeldViewmodel')
		rService:BindToRenderStep('Aim', Enum.RenderPriority.Camera.Value, function(deltaTime)
			local baseCameraCFrame = GetCameraYRotation()
			local targetPrimaryCFrame = baseCameraCFrame * aimOffset:Inverse()

			local alpha = math.clamp(deltaTime * speed, 0, 1)
			local newCFrame = viewmodel:GetPivot():Lerp(targetPrimaryCFrame, alpha)

			viewmodel:PivotTo(newCFrame)
			--utility.StopViewmodelAnimations(viewmodel, 'Ready')
		end)

		aim:FireServer(true)

		mouse.Button2Up:Once(function()
			rService:UnbindFromRenderStep('Aim')
			rService:UnbindFromRenderStep('AimReturn')
			aim:FireServer(false)

			local returnSpeed = 3 / vanillaAimSpeed
			rService:BindToRenderStep('AimReturn', Enum.RenderPriority.Camera.Value, function(deltaTime)
				local baseCameraCFrame = GetCameraYRotation()
				local targetOriginalCFrame = baseCameraCFrame * cameraOffset

				local currentCFrame = viewmodel:GetPivot()
				local alpha = math.clamp(deltaTime * returnSpeed, 0, 1)
				local newCFrame = currentCFrame:Lerp(targetOriginalCFrame, alpha)

				viewmodel:PivotTo(newCFrame)

				if (newCFrame.Position - targetOriginalCFrame.Position).Magnitude < 0.1 then
					viewmodel:PivotTo(targetOriginalCFrame)
					rService:UnbindFromRenderStep('AimReturn')
					utility.ReweldViewmodel(viewmodel, camera)
				end
			end)
		end)
	end,

i definitely didn’t use AI i swear

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