Does anyone know what could be causing this annoying issue??
local ViewportFrame = script.Parent.Parent.ViewportFrame
local worldModel = script.Parent.WorldModel
local uis = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local mainFrame = script.Parent.Parent.Parent.Container
local refreshAvatar = game.ReplicatedStorage.RefreshAvatarOutfit
-- Convert angles to radians for use in trigonometric functions
local function degToRad(degrees)
return degrees * (math.pi / 180)
end
-- Define the function to clone and add the local player's character to the viewport frame
local function AddClonedPlayerCharacterToViewportFrame()
-- Customizable Parameters
local rotationSpeed = 0.5
local cameraDistance = 5 -- Distance from the character
local initialYaw = 195 -- Initial horizontal angle (yaw) in degrees
local initialPitch = 20 -- Initial vertical angle (pitch) in degrees
-- Local Variables
local inputDown = false
local lastInputPosition = nil
-- Fetch and clone the local player's character
local originalCharacter = player.Character or player.CharacterAdded:Wait()
player.Character.Archivable = true
local clonedCharacter = originalCharacter:Clone()
-- Clear existing children and set up the camera
local WorldModel = ViewportFrame.WorldModel
WorldModel:ClearAllChildren()
if ViewportFrame.CurrentCamera then
ViewportFrame.CurrentCamera:Destroy()
end
-- Parent the cloned character to the WorldModel
clonedCharacter.Parent = WorldModel
clonedCharacter:PivotTo(CFrame.new(0, 0, 0))
-- Set HumanoidRootPart as PrimaryPart
clonedCharacter.PrimaryPart = clonedCharacter:WaitForChild('HumanoidRootPart')
-- Ensure the character is not lifted
clonedCharacter:SetPrimaryPartCFrame(CFrame.new(0, 0, 0))
-- Create and set up the camera
local Camera = Instance.new('Camera')
Camera.CameraType = Enum.CameraType.Scriptable
ViewportFrame.CurrentCamera = Camera
-- Calculate camera position based on initial angles
local yaw = degToRad(initialYaw)
local pitch = degToRad(initialPitch)
local offset = Vector3.new(
cameraDistance * math.cos(pitch) * math.sin(yaw),
cameraDistance * math.sin(pitch),
cameraDistance * math.cos(pitch) * math.cos(yaw)
)
local cameraPosition = clonedCharacter.PrimaryPart.Position + offset
-- Set the camera position and orientation
Camera.CFrame = CFrame.new(cameraPosition, clonedCharacter.PrimaryPart.Position)
-- Function to check if the viewport is hovered
local function isViewportHovered()
local mouse = player: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
-- Function to find the current character's PrimaryPart
local function getPrimaryPart()
local character = WorldModel:FindFirstChildOfClass("Model")
if character and character:FindFirstChild("HumanoidRootPart") then
return character.HumanoidRootPart
end
return nil
end
-- Function to update camera angles based on input
local function updateRotation(input)
if inputDown and isViewportHovered() then
if lastInputPosition then
local primaryPart = getPrimaryPart()
if primaryPart then
local deltaX = (input.Position.X - lastInputPosition.X) * rotationSpeed
local deltaY = (input.Position.Y - lastInputPosition.Y) * rotationSpeed
-- Update yaw and pitch
initialYaw = initialYaw - deltaX
initialPitch = math.clamp(initialPitch + deltaY, -80, 80) -- Limiting vertical angle
-- Update camera position
local yaw = degToRad(initialYaw)
local pitch = degToRad(initialPitch)
local offset = Vector3.new(
cameraDistance * math.cos(pitch) * math.sin(yaw),
cameraDistance * math.sin(pitch),
cameraDistance * math.cos(pitch) * math.cos(yaw)
)
local cameraPosition = primaryPart.Position + offset
Camera.CFrame = CFrame.new(cameraPosition, primaryPart.Position)
lastInputPosition = input.Position
else
print("Warning: PrimaryPart not found!")
end
else
print("Warning: lastInputPosition is nil!")
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
if isViewportHovered() then
inputDown = true
lastInputPosition = input.Position
end
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) and inputDown then
if isViewportHovered() then
updateRotation(input)
else
inputDown = false
end
end
end)
-- Mouse and touch hover events
ViewportFrame.MouseEnter:Connect(function()
-- Handle mouse enter event if needed
end)
ViewportFrame.MouseLeave:Connect(function()
-- Handle mouse leave event if needed
inputDown = false
lastInputPosition = nil
end)
end
-- Monitor the Visibility property of mainFrame
mainFrame.Changed:Connect(function(property)
if property == "Visible" and mainFrame.Visible then
AddClonedPlayerCharacterToViewportFrame()
end
end)
-- Refresh the avatar when the event is triggered
refreshAvatar.Event:Connect(function()
AddClonedPlayerCharacterToViewportFrame()
end)