Character looking towards mouse buggy and inverted

Wondering what I would do to fix the issue of the character not looking towards the mouse correctly as shown in the video, currently using character:pivotto()

Also if possible I may as well ask, How do I correctly use math.clamp() in this situation to prevent the character spinning out of control when directly looking down as show in the video at the end

Mouse script:

	connection = RunService.RenderStepped:Connect(function(dt)
		character:PivotTo(CF * CFrame.Angles(CFrame.new(character.HumanoidRootPart.Position, mouse.Hit.Position):ToOrientation()))
	end)
end

local function fluxFollowsMouse()
	local folderToFilter = workspace.Debris
	mouse.TargetFilter = folderToFilter
	
	local boxPart = Instance.new("Part")
	boxPart.Parent = debrisFolder
	boxPart.CanCollide = false
	boxPart.Transparency = 0.5
	boxPart.Size = Vector3.new(10,10,10)
	
	
	RunService.RenderStepped:Connect(function(dt)
		local x = mouse.Hit.X
		local y = mouse.Hit.Y + boxPart.Size.Y / 2
		local z = mouse.Hit.Z
		local vector3 = Vector3.new(x,y,z)
		
		boxPart.Position = vector3
	end)
end

local function LiftCharacter(character)
	local CF = character.HumanoidRootPart.CFrame * CFrame.new(0,15,0)
	
	local CFrameValue = Instance.new("CFrameValue")
	CFrameValue.Value = character:GetPivot()

	CFrameValue.Changed:Connect(function(newValue)
		character:PivotTo(newValue * CFrame.Angles(CFrame.new(character.HumanoidRootPart.Position, mouse.Hit.Position):ToOrientation()))
	end)

	local tweenInfo = TweenInfo.new(1.5, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)

	local tween = TweenService:Create(CFrameValue, tweenInfo, {Value = CF})
	tween:Play()

	tween.Completed:Connect(function()
		CFrameValue:Destroy()
		lookToMouse(character, CF)
	end)
end

function GraviticFlux.RunFlux(character)
	character.HumanoidRootPart.Anchored = true
	
	fluxFollowsMouse()
	LiftCharacter(character)
end

2 Likes