Unexpected fling or dashing When Jumping

I’m having an issue where my character sometimes dashes or fling in a random direction when I’m moving backward or sideways and spam jumping. This only happens when my custom ViewModel is active and being pivoted to the camera’s CFrame

RenderConnection = RunService.RenderStepped:Connect(function(dt)
		if not CurrentViewmodel or not CurrentViewmodel.PrimaryPart then return end
		
		if IsFirstPerson() then
			UserInputService.MouseIconEnabled = false
			local camCF = Camera.CFrame

			for _, descendant in ipairs(Gun:GetDescendants()) do
				if descendant:IsA("BasePart") then
					descendant.LocalTransparencyModifier = 1
				elseif descendant:IsA("Texture") or descendant:IsA("Decal") then
					descendant.Transparency = 1
				elseif descendant:IsA("SurfaceGui") then
					descendant.Enabled = false
				end
			end

			for _, part in ipairs(CurrentViewmodel:GetDescendants()) do
				if part:IsA("BasePart") and part.Name ~= "FakeCamera" then
					part.LocalTransparencyModifier = 0
				end
			end			

			for i, Parts in ipairs(CurrentViewmodel:GetDescendants()) do
				if Parts:IsA("BasePart") then
					Parts.CanCollide = false
				end
			end

			local rayOrigin = camCF.Position
			local rayDirection = camCF.LookVector * 5.5

			local params = RaycastParams.new()
			params.FilterDescendantsInstances = {CurrentViewmodel, Players.LocalPlayer.Character}
			params.FilterType = Enum.RaycastFilterType.Blacklist
			params.IgnoreWater = true

			local result = workspace:Raycast(rayOrigin, rayDirection, params)

			local pushbackCF = CFrame.new()
			local maxDistance = 5.5
			local maxPushbackDepth = 3
			local targetPushAmount = 0

			if result then
				local distance = (result.Position - rayOrigin).Magnitude
				local closeness = 1 - math.clamp(distance / maxDistance, 0, 1)
				targetPushAmount = closeness * maxPushbackDepth
			end

			pushbackCF = pushbackCF:Lerp(CFrame.new(0, 0, -targetPushAmount), 3):Inverse()

			local targetFOV = IsAiming and AimFOV or DefaultFOV
			CurrentFOV += (targetFOV - CurrentFOV) * 0.15
			Camera.FieldOfView = CurrentFOV

			local delta = UserInputService:GetMouseDelta()
			local swaySpeed = IsAiming and 0.05 or 0.1
			local swayX = math.clamp(-delta.X / 150, -swaySpeed, swaySpeed)
			local swayY = math.clamp(-delta.Y / 150, -swaySpeed, swaySpeed)

			ViewSwayCF = IsAiming and ViewSwayCF:Lerp(CFrame.new(), 0.3)
				or ViewSwayCF:Lerp(CFrame.Angles(swayY, swayX, 0), 0.2)

			local char = Players.LocalPlayer.Character
			local hrp = char and char:FindFirstChild("HumanoidRootPart")
			local humanoid = char and char:FindFirstChildOfClass("Humanoid")
			if not hrp or not humanoid then return end

			local velocity = hrp.Velocity
			local horizontalSpeed = Vector3.new(velocity.X, 0, velocity.Z).Magnitude

			local rightVector = hrp.CFrame.RightVector
			local lateralVelocity = hrp.Velocity:Dot(rightVector)

			local targetStrafeTilt = math.clamp(lateralVelocity / 50, -1, 1)
			StrafeTilt = StrafeTilt + (targetStrafeTilt - StrafeTilt) * 0.1

			SmoothedSpeed += (horizontalSpeed - SmoothedSpeed) * 0.1

			local isMoving = SmoothedSpeed > 1
			local walkCycleSpeed = isMoving and (SmoothedSpeed / 10) or 0
			SmoothWalkSpeed = SmoothWalkSpeed or 0
			SmoothWalkSpeed += (walkCycleSpeed - SmoothWalkSpeed) * 0.1
			BobbleTime += dt * SmoothWalkSpeed

			local maxBobMultiplier = 2
			local maxSpeed = 40

			local ZiplineState = game.Players.LocalPlayer.Character:WaitForChild("State").Zipline
			local Sliding = game.Players.LocalPlayer.Character:WaitForChild("State").Sliding
			local speedFactor = math.clamp(SmoothedSpeed / maxSpeed, 0, 0.3)

			local bobMultiplier
			if IsAiming or ZiplineState.Value or Sliding.Value then
				bobMultiplier = 0
			else
				bobMultiplier = speedFactor * maxBobMultiplier
			end

			local walkBobX = math.sin(BobbleTime * 8) * 0.05 * bobMultiplier
			local walkBobZ = math.sin(BobbleTime * 8 + math.pi / 2) * 0.02 * bobMultiplier
			local tiltRoll = math.sin(BobbleTime * 8) * math.rad(1.5) * bobMultiplier
			local jitterX = (math.noise(BobbleTime * 0.4, 0, 0) - 0.5) * 0.005

			local ViewBobCF = CFrame.new(walkBobX, 0 + LandingOffset, walkBobZ)
				* CFrame.Angles(0, 0, tiltRoll)
			CFrame.new(jitterX, 0, 0)
			local currentY = hrp.Position.Y

			local smoothFactor = IsAiming and 0.005 or 0.15
			if LastCharY then
				local deltaY = currentY - LastCharY
				local targetOffset = -deltaY * 2
				targetOffset = math.clamp(targetOffset, -0.5, 0.5)
				VerticalOffset += (targetOffset - VerticalOffset) * smoothFactor
			end
			LastCharY = currentY
			VerticalOffset += (0 - VerticalOffset) * 0.05

			local config = Gun:FindFirstChild("Config")
			local isThrowable = config and config:FindFirstChild("Throwable")
			local isActuallyThrowable = isThrowable and isThrowable.Value

			if not isThrowable then			
			end

			if IsAiming and AimPart and AimPart:IsDescendantOf(CurrentViewmodel) and not isActuallyThrowable then
				local aimOffset = AimPart.CFrame:ToObjectSpace(CurrentViewmodel.PrimaryPart.CFrame)
				AimCF = AimCF:Lerp(aimOffset, 0.5)
			else
				AimCF = AimCF:Lerp(CFrame.new(), 0.15)
			end

			local _, lastYaw = LastCameraCF:ToEulerAnglesYXZ()
			local _, currentYaw = camCF:ToEulerAnglesYXZ()
			local yawDelta = math.clamp(currentYaw - lastYaw, -0.15, 0.15)
			SmoothedTilt = SmoothedTilt + (yawDelta - SmoothedTilt) * 0.1
			local TiltCF = CFrame.Angles(0, 0, -SmoothedTilt * (IsAiming and 2 or 4) + math.rad(-10 * StrafeTilt))

			local ViewOffset = IsAiming and AimOffset or DefaultOffset
			local ViewOffsetCF = CFrame.new(ViewOffset.Position)

			LastCameraCF = camCF

			-- Recoil
			RecoilVelocity *= 0.85
			RecoilOffset *= CFrame.Angles(math.rad(RecoilVelocity.Y), 0, 0) * CFrame.new(0, 0, RecoilVelocity.Z)
			RecoilOffset = RecoilOffset:Lerp(CFrame.new(), 0.3)

			ViewKickVelocity *= 0.85
			ViewKickOffset *= CFrame.Angles(math.rad(ViewKickVelocity.X), math.rad(ViewKickVelocity.Y), 0)
				* CFrame.new(0, 0, ViewKickVelocity.Z)
			ViewKickOffset = ViewKickOffset:Lerp(CFrame.new(), 0.3)

			camCF = camCF * ViewKickOffset

			local SlideNoAim = Sliding.Value and not IsAiming

			local targetSlideOffset = SlideNoAim and CFrame.new(0, -0.3, 0) or CFrame.new()
			local targetSlideRotation = Sliding.Value and CFrame.Angles(math.rad(-1), 0, 0) or CFrame.new()

			SlideOffset = SlideOffset:Lerp(targetSlideOffset, 0.1)
			SlideRotation = SlideRotation:Lerp(targetSlideRotation, 0.1)
			local LandingCF = CFrame.new(0, LandingOffset, 0)
			local YMotionCF = CFrame.new(0, VerticalOffset, 0)

			local finalCF = camCF
				* TiltCF
				* ViewSwayCF
				* ViewBobCF
				* pushbackCF
				* RecoilOffset
				* YMotionCF
				* SlideOffset
				* SlideRotation
				* AimCF

			if CurrentFakeCamera and CurrentViewmodel.PrimaryPart then
				local NewCamCF = CurrentFakeCamera.CFrame:ToObjectSpace(CurrentViewmodel.PrimaryPart.CFrame)

				if OldCamCF then
					local _, _, Z = NewCamCF:ToOrientation()
					local X, Y, _ = NewCamCF:ToObjectSpace(OldCamCF):ToEulerAnglesXYZ()
					Camera.CFrame = Camera.CFrame * CFrame.Angles(X, Y, -Z)
				end
				OldCamCF = NewCamCF
			end

			CurrentViewmodel:PivotTo(finalCF)
		else
			for _, descendant in ipairs(Gun:GetDescendants()) do
				if descendant:IsA("BasePart") then
					descendant.LocalTransparencyModifier = 0
				elseif descendant:IsA("Texture") or descendant:IsA("Decal") then
					descendant.Transparency = 0
				elseif descendant:IsA("SurfaceGui") then
					descendant.Enabled = true
				end
			end

			for _, part in ipairs(Gun:GetDescendants()) do
				if part:IsA("BasePart") then part.LocalTransparencyModifier = 0 end
			end

			UserInputService.MouseIconEnabled = true
		end		
	end)

Video:

External Media
1 Like

Maybe make your viewmodel has no collision include mass

Sometimes AutoRotate can cause behavior like this.

Does it have any collision?

no, i have a function that turns all Parts collision group to viewmodel. still didnt fixed it