Rotating character to mouse is not working right

  1. What do you want to achieve? Keep it simple and clear!
    I am making a top down shooter game and I’m trying to make it so that the character will rotate to the direction the mouse is pointing at

  2. What is the issue? Include screenshots / videos if possible!
    It kinda works but it have a problem where the character sinks to the ground for some reason

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Tried searching but still find no solution

game:GetService("RunService").RenderStepped:Connect(function()
	script.Parent:SetPrimaryPartCFrame(CFrame.new(script.Parent.HumanoidRootPart.CFrame.Position, Vector3.new(game.Players.LocalPlayer:GetMouse().Hit.Position.X, script.Parent.HumanoidRootPart.CFrame.Position.Y, game.Players.LocalPlayer:GetMouse().Hit.Position.Z)))
end)
1 Like

Try this out:

local PS = game:GetService("Players")
local RNS = game:GetService("RunService")

local player = PS.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local HRP = char:WaitForChild("HumanoidRootPart")
local mouse = player:GetMouse()

RNS.RenderStepped:Connect(function()
	HRP.CFrame = CFrame.lookAt(HRP.Position, Vector3.new(mouse.Hit.Position.X, HRP.Position.Y, mouse.Hit.Position.Z))
end)

local Game = game
local RunService = Game:GetService("RunService")
local Players = Game:GetService("Players")
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()
local Character = Player.Character or Player.CharacterAdded:Wait()

local function OnRenderStep()
	local Pivot = Character:GetPivot()
	Character:PivotTo(CFrame.lookAt(Pivot.Position, Vector3.new(Mouse.Hit.X, Pivot.Y, Mouse.Hit.Z)))
end

RunService.RenderStepped:Connect(OnRenderStep)
1 Like