Rotating model in viewport frame

Current Behaviour:

Wanted Behaviour:

Code:

	local function AddToViewportFrame(Character)
		-- Local Variables
		local rotationSpeed = 0.0135  -- Adjust this value to control rotation speed
		local inputDown = false
		local lastInputPosition = nil

		-- Clear existing children and set up the camera
		local WorldModel = ViewportFrame.WorldModel
		WorldModel:ClearAllChildren()

		if ViewportFrame.CurrentCamera then
			ViewportFrame.CurrentCamera:Destroy()
		end

		-- Parent the character to the WorldModel
		Character.Parent = WorldModel
		Character:PivotTo(CFrame.new(0, 0, 0))

		-- Create and set up the camera
		local Camera = Instance.new('Camera')
		Camera.CameraType = Enum.CameraType.Scriptable
		Camera.CFrame = CFrame.new(
			(Character:WaitForChild('HumanoidRootPart').CFrame * CFrame.new(0, 1, -5)).Position,
			Character.HumanoidRootPart.Position
		)
		ViewportFrame.CurrentCamera = Camera

		-- Ensure all parts are unanchored
		for _, Descendant in pairs(Character:GetDescendants()) do
			if Descendant:IsA('BasePart') or Descendant:IsA('Part') or Descendant:IsA('MeshPart') then
				Descendant.Anchored = false
			end    
		end

		-- Add Animator if it doesn't exist
		local Humanoid = Character:WaitForChild('Humanoid')
		local Animator = Humanoid:FindFirstChild('Animator')
		if not Animator then
			Animator = Instance.new('Animator')
			Animator.Parent = Humanoid
		end

		-- Create and play animation
		local Animation = Instance.new('Animation')
		Animation.AnimationId = 'http://www.roblox.com/asset/?id=10921259953'
		local AnimationTrack = Animator:LoadAnimation(Animation)
		AnimationTrack.Looped = true
		AnimationTrack.Priority = Enum.AnimationPriority.Action4
		AnimationTrack:Play()

		local function isViewportHovered()
			local mouse = game.Players.LocalPlayer:GetMouse()
			return ViewportFrame.AbsolutePosition.X <= mouse.X and mouse.X <= ViewportFrame.AbsolutePosition.X + ViewportFrame.AbsoluteSize.X
				and ViewportFrame.AbsolutePosition.Y <= mouse.Y and mouse.Y <= ViewportFrame.AbsolutePosition.Y + ViewportFrame.AbsoluteSize.Y
		end

		local maxVerticalAngle = math.rad(45)  -- Convert 45 degrees to radians

		local function updateRotation(input)
			if inputDown and isViewportHovered() then
				if lastInputPosition then
					local deltaX = (input.Position.X - lastInputPosition.X) * rotationSpeed
					local deltaY = (input.Position.Y - lastInputPosition.Y) * rotationSpeed

					-- Update the character property every time we rotate
					local PrimaryPart = Character.PrimaryPart
					if PrimaryPart then
						local currentCFrame = PrimaryPart.CFrame
						local _, _, _, _, currentYRotation, currentXRotation = currentCFrame:ToEulerAnglesYXZ()

						-- Ensure default values if the rotations are nil
						currentYRotation = currentYRotation or 0
						currentXRotation = currentXRotation or 0

						-- Calculate new rotation angles
						local newYRotation = (currentYRotation or 0) + deltaX
						local newXRotation = math.clamp((currentXRotation or 0) + deltaY, -maxVerticalAngle, maxVerticalAngle)

						-- Create a new rotation CFrame
						local newCFrame = CFrame.Angles(newXRotation, newYRotation, 0)
						PrimaryPart.CFrame = currentCFrame * newCFrame

						lastInputPosition = input.Position
					end
				end
			end
		end


		-- Input events for mouse and touch
		uis.InputBegan:Connect(function(input)
			if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
				inputDown = true
				lastInputPosition = input.Position
			end
		end)

		uis.InputEnded:Connect(function(input)
			if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
				inputDown = false
				lastInputPosition = nil
			end
		end)

		uis.InputChanged:Connect(function(input)
			if input.UserInputType == Enum.UserInputType.MouseMovement or input.UserInputType == Enum.UserInputType.Touch then
				updateRotation(input)
			end
		end)

		-- Mouse and touch hover events
		ViewportFrame.MouseEnter:Connect(function()
			-- Mouse enter event can be used if you need to track when the mouse enters
		end)

		ViewportFrame.MouseLeave:Connect(function()
			-- Mouse leave event can be used if you need to handle when the mouse leaves
			inputDown = false
			lastInputPosition = nil
		end)

		-- Handle touch inputs to set hover state for mobile devices
		ViewportFrame:GetPropertyChangedSignal("AbsolutePosition"):Connect(function()
			if isViewportHovered() then
				-- Handle touch tap
			end
		end)

		ViewportFrame.TouchTap:Connect(function()
			-- Handle touch taps if needed
		end)
	end

Can anyone help me with character rotation ?

It would probably be better to rotate the camera around the HumanoidRootPart instead.

Try using CFrame.fromOrientation instead, the order of the angles can matter at times.

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