Is there a way i can make the player character pivot to where the player mouse is at?

Basically i was bored so i was wondering if i could make a game where the mechanism is close to an obby but you have to use your mouse to make the player go the right spots.

i encontered an issue which make the player character flying when making it and since i’m not really associated in the use of CFrames i need help to fix.

Btw ignore the comment for the sendnotification, was for a bug report but the bug did not happen again.

local mouse = game.Players.LocalPlayer:GetMouse()

--mouse.Button1Down:Connect(function()
--	game.StarterGui:SetCore("SendNotification",{Title = "test", Text = "hi",Button1 = "Accept",Button2 = "Decline"})
--end)

local cooldown = false
mouse.Move:Connect(function()
	if not cooldown then
		cooldown = true
		
		if game.Players.LocalPlayer.Character then
			game.Players.LocalPlayer.Character:PivotTo(mouse.Hit)
		end
		
		wait(0.07)
		cooldown = false
	end
end)
1 Like

At the actual time, the script make the player behave like this, making the game unplayable.

2 Likes
Code
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local runService = game:GetService("RunService")

local character = player.Character or player.CharacterAdded:Wait()
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local camera = workspace.CurrentCamera

local mouseSensitivity = 0.2 -- Adjust rotation sensitivity (lower = slower)
local rotationSpeed = 1 -- Maximum rotation per frame in degrees (limits abrupt turning)
local safeZoneRadius = 25 -- Distance in studs within which the character won't rotate

-- Store the current rotation angle of the character
local currentRotation = humanoidRootPart.CFrame.LookVector

-- Function to update character and camera rotation
local function updateRotation()
	if character and humanoidRootPart and camera then
		-- Get the mouse position in the world
		local mousePosition = mouse.Hit.Position
		
		-- Calculate the distance between the mouse and the character's center
		local characterPosition = humanoidRootPart.Position
		local distanceToMouse = (Vector3.new(mousePosition.X, characterPosition.Y, mousePosition.Z) - characterPosition).Magnitude
		
		-- Check if the mouse is outside the safe zone
		if distanceToMouse > safeZoneRadius then
			-- Calculate the target direction from the character to the mouse
			local targetDirection = Vector3.new(mousePosition.X, characterPosition.Y, mousePosition.Z) - characterPosition
			targetDirection = targetDirection.Unit
			
			-- Interpolate rotation for smoother movement
			local newDirection = currentRotation:Lerp(targetDirection, mouseSensitivity)
			newDirection = Vector3.new(newDirection.X, 0, newDirection.Z).Unit -- Keep horizontal rotation only
			
			-- Apply rotation with constraints
			local angleDifference = math.deg(math.acos(currentRotation:Dot(newDirection)))
			if angleDifference > rotationSpeed then
				newDirection = currentRotation:Lerp(newDirection, rotationSpeed / angleDifference)
			end
			
			-- Update the character rotation
			humanoidRootPart.CFrame = CFrame.new(characterPosition, characterPosition + newDirection)
			currentRotation = newDirection
		end
		
		-- Update camera position and rotation
		local offset = Vector3.new(0, 5, 12) -- Adjust these values to position the camera relative to the character
		local cameraPosition = humanoidRootPart.Position - humanoidRootPart.CFrame.LookVector * offset.Z + Vector3.new(0, offset.Y, 0)
		camera.CFrame = CFrame.new(cameraPosition, humanoidRootPart.Position + humanoidRootPart.CFrame.LookVector)
	end
end

-- Connect the function to RenderStepped for smooth updates
runService.RenderStepped:Connect(updateRotation)

@MagicLuau

Your’ script offering an overall glitchy experience, tryed to disable movement and adding a jump feature but still not feeling smooth.

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local runService = game:GetService("RunService")
local imputservice = game:GetService("UserInputService")

local character = player.Character or player.CharacterAdded:Wait()
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")

local camera = workspace.CurrentCamera

local mouseSensitivity = 0.4 -- Adjust rotation sensitivity (lower = slower)
local rotationSpeed = 1 -- Maximum rotation per frame in degrees (limits abrupt turning)
local safeZoneRadius = 25 -- Distance in studs within which the character won't rotate

require(player.PlayerScripts.PlayerModule.ControlModule):Disable()

workspace.Camera.CameraType = Enum.CameraType.Scriptable

-- Store the current rotation angle of the character
local currentRotation = humanoidRootPart.CFrame.LookVector

mouse.Button1Down:Connect(function()
	character.Humanoid:MoveTo(mouse.Hit.Position)
end)

imputservice.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.Space then
		character.Humanoid.Jump = true
	end
end)

-- Function to update character and camera rotation
local function updateRotation()
	if character and humanoidRootPart and camera then
		-- Get the mouse position in the world
		local mousePosition = mouse.Hit.Position

		-- Calculate the distance between the mouse and the character's center
		local characterPosition = humanoidRootPart.Position
		local distanceToMouse = (Vector3.new(mousePosition.X, characterPosition.Y, mousePosition.Z) - characterPosition).Magnitude

		-- Check if the mouse is outside the safe zone
		if distanceToMouse > safeZoneRadius then
			-- Calculate the target direction from the character to the mouse
			local targetDirection = Vector3.new(mousePosition.X, characterPosition.Y, mousePosition.Z) - characterPosition
			targetDirection = targetDirection.Unit

			-- Interpolate rotation for smoother movement
			local newDirection = currentRotation:Lerp(targetDirection, mouseSensitivity)
			newDirection = Vector3.new(newDirection.X, 0, newDirection.Z).Unit -- Keep horizontal rotation only

			-- Apply rotation with constraints
			local angleDifference = math.deg(math.acos(currentRotation:Dot(newDirection)))
			if angleDifference > rotationSpeed then
				newDirection = currentRotation:Lerp(newDirection, rotationSpeed / angleDifference)
			end

			-- Update the character rotation
			humanoidRootPart.CFrame = CFrame.new(characterPosition, characterPosition + newDirection)
			currentRotation = newDirection
		end

		-- Update camera position and rotation
		local offset = Vector3.new(0, 20, 12) -- Adjust these values to position the camera relative to the character
		local cameraPosition = humanoidRootPart.Position - humanoidRootPart.CFrame.LookVector * offset.Z + Vector3.new(0, offset.Y, 0)
		camera.CFrame = CFrame.new(cameraPosition, humanoidRootPart.Position + humanoidRootPart.CFrame.LookVector)
	end
end

-- Connect the function to RenderStepped for smooth updates
runService.RenderStepped:Connect(updateRotation)

So, you essentially want to replicate the pre-existing Click-to-move feature? Most roundabout way I can think of is to use PathfindingService and Humanoid:MoveTo while using GetMouseLocation and ViewportPointToRay in order to find the mouse’s location in the world space. That was quite a verbose way to put it but I genuinely couldn’t find better, shorter wording.

If you want that in video, watch this snippet from Suphi’s video that implements the latter part of my comment. Meanwhile PathfindingService has more than enough documentation.

Edit: you could also see how the engine implements the feature themselves. Should be in the player controls script or something.

its fine, im going to do another system and get started with this one later