Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to rotate the player's character to face the mouse?

local Player = game:GetService("Players").LocalPlayer
local Cursor = Player:GetMouse()

Cursor.Move:Connect(function()
    local Character = Player.Character or Player.CharacterAdded:Wait()
    local Humanoid = Character:WaitForChild("Humanoid")
    if (Humanoid:GetState() ~= Enum.HumanoidStateType.Dead) then
        local CursorPosition = Cursor.Hit.p
        local CenterPosition = Character.Torso.Position
        local LookVector = Vector3.new(CursorPosition.X,CenterPosition.Y, CursorPosition.Z)
        Character.Torso.CFrame = CFrame.new(CenterPosition, LookVector)
    end
    Humanoid.AutoRotate = false
end)

Works, but is choppy. It moves instantly. I tried using tweenservice to make it smooth, but nothing I tried worked.

local Player = game:GetService("Players").LocalPlayer
local Cursor = Player:GetMouse()
local TweenService = game:GetService("TweenService")

Cursor.Move:Connect(function()
    local Character = Player.Character or Player.CharacterAdded:Wait()
    local Humanoid = Character:WaitForChild("Humanoid")
    if (Humanoid:GetState() ~= Enum.HumanoidStateType.Dead) then
        local CursorPosition = Cursor.Hit.p
        local CenterPosition = Character.Torso.Orientation
        local LookVector = Vector3.new(CursorPosition.X,CenterPosition.Y, CursorPosition.Z)	
		local goal = {CFrame = CFrame.new(CenterPosition, LookVector)}
		local tweeninfo = TweenInfo.new(0.5,Enum.EasingStyle.Linear,Enum.EasingDirection.In,0,false,0)
		local tweenlook = TweenService:Create(Character.Torso,tweeninfo,goal)
		tweenlook:Play()
    end
    Humanoid.AutoRotate = false
end)

I see some values changing, but nothing happens. Anyone know more about this or if there's a better method to make it smoother?

1 answer

Here, put it in a localscript, and put the localscript into StarterGUI.

-- [[Variables]] --
local Player = game.Players.LocalPlayer
repeat wait() until Player.Character
local Mouse = Player:GetMouse()

-- [[Events]] --
game:GetService("RunService").RenderStepped:Connect(function()
	local Character = Player.Character
	local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
    HumanoidRootPart.CFrame = CFrame.new(HumanoidRootPart.CFrame.p, HumanoidRootPart.CFrame.p + Vector3.new(Mouse.hit.lookVector.x, 0, Mouse.hit.lookVector.z))
end)

1 comment

0

Doesn't work. Doesn't rotate fully, and it's still choppy.

This discussion is preserved as it was. New answers, comments, and votes are closed.