when the mouse changes its position the player is supposed to be looking at the mouse but instead got this weird behavior
here is the code btw:
local character = script.Parent.Parent
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local RunService = game:GetService("RunService")
local HumanoidRootPart = character.HumanoidRootPart
RunService.RenderStepped:Connect(function()
local Rotation = CFrame.new(math.rad(Mouse.X), HumanoidRootPart.CFrame.Y, HumanoidRootPart.CFrame.Z)
HumanoidRootPart.CFrame = Rotation
end)
almost got it right but it spins like crazy like wth
local character = script.Parent.Parent
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local RunService = game:GetService("RunService")
local HumanoidRootPart = character.HumanoidRootPart
RunService.RenderStepped:Connect(function()
-- lets try lol
local mousePos = Mouse.Hit.Position
HumanoidRootPart.CFrame = HumanoidRootPart.CFrame * CFrame.Angles(0, math.rad(mousePos.Y), 0)
end)
The latest code you provided in this thread is adding rotation to the current CFrame of the HumanoidRootPart, so then that means it’s rotating continously.
The workaround to this is to only get the position, and add rotation to that.
You may also want the character to point at where the cursor is pointing at? The code above only sets the rotation to where the cursor is in the X axis in the workspace. Which will cause really weird behavior.
Use this instead.
In this code the CFrame has 2 arguments in it. The first one is the origin, and the other is lookAt, so the CFrame calculates where to point at. You may not want to apply any other orientations other than Y because some weird behavior happens such as character rotating downwards/upwards.
-- Get the euler angles, Y is the only one needed.
local rx, ry, rz = CFrame.new(HumanoidRootPart.Position, mousePos):ToEulerAnglesYXZ()
-- Remember that this returns radians, so you have to use math.deg to convert it to degrees.
-- But since CFrame.Angles uses radians, there is no need for conversion.
-- Apply CFrame to the HumanoidRootPart
HumanoidRootPart.CFrame = CFrame.new(HumanoidRootPart.Position) * CFrame.Angles(0, ry, 0)
A :Wait() is not needed in a RenderStepped event, this event fires when each frame has “stepped”.