My Viewportframe not showing the correct animation

Im trying to make the rig look on the position of the mouse 9 and base of the position the rig to rotate .

The problem is that the rig rotate on the other side of the mouse position.


I try adjusting the values but it just get worse. ( im new to that and i dont perfectlly understand how to make it correct )

local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")

local viewportFrame = script.Parent
local camera = Instance.new("Camera")
local model = viewportFrame:WaitForChild("Rig")

viewportFrame.CurrentCamera = camera
camera.CFrame = CFrame.new(Vector3.new(0, 2, -9), Vector3.new(0, 0, 0))

if model.PrimaryPart then
	model:PivotTo(CFrame.new(0, -2, 0))
end

local function getMouseDirection(screenPoint)
	local viewportSize = viewportFrame.AbsoluteSize
	local viewportPosition = viewportFrame.AbsolutePosition

	local centerX = viewportPosition.X + (viewportSize.X / 2)
	local centerY = viewportPosition.Y + (viewportSize.Y / 2)

	local deltaX = screenPoint.X - centerX
	local deltaY = screenPoint.Y - centerY

	local totalDist = math.sqrt(deltaX * deltaX + deltaY * deltaY)
	if totalDist == 0 then return 0 end

	local normalizedX = deltaX / totalDist
	local normalizedY = deltaY / totalDist

	local angle = -math.atan2(normalizedX, normalizedY)
	return angle
end

RunService.RenderStepped:Connect(function()
	local mouseLocation = UserInputService:GetMouseLocation()
	local angle = getMouseDirection(mouseLocation)

	if model.PrimaryPart then
		local modelPos = model:GetPrimaryPartCFrame().Position
		local targetCFrame = CFrame.new(modelPos) * CFrame.Angles(0, angle, 0)
		model:SetPrimaryPartCFrame(targetCFrame)
	end
end)

If you need more info ask bcs i try alot of stuff but there is not much information abouth that .

I changed this:

local angle = -math.atan2(normalizedX, normalizedY)

to this:

local angle = -math.atan2(-1 * normalizedX, normalizedY)

and it follows my cursor correctly.

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