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

How would I make a character face towards the mouse position?

I'm wondering how to make it so your character will face whatever direction your mouse is.

Would I change the orientation like this

HumanoidRootPart.Orientation = Vector3.new(mouse.X,mouse.Y,0)

and do that everytime the mouse moves or is there a different way?

1 comment

0

Classic

1 answer

✓ Accepted answer

Local Script in StarterGui

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

mouse.Move:Connect(function()
	if player.Character ~= nil and workspace:FindFirstChild(player.Name) ~= nil then
		local root = player.Character.HumanoidRootPart
		player.Character:SetPrimaryPartCFrame(CFrame.new(root.Position, (mouse.Hit.p * Vector3.new(1,0,1)) + (root.Position * Vector3.new(0, 1, 0))))
	end
end)

The function simply checks to see if the mouse is moving and if the player's character is loaded.

SetPrimaryPartCFrame moves the entire player (since the character is a model)

The above example would be a continuous variant, but the main part to see that the player faces the mouse is this:

local root = player.Character.HumanoidRootPart
player.Character:SetPrimaryPartCFrame(CFrame.new(root.Position, (mouse.Hit.p * Vector3.new(1,0,1)) + (root.Position * Vector3.new(0, 1, 0))))

COMMENT EDIT

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

repeat wait() until player.Character ~= nil and workspace:FindFirstChild(player.Name) ~= nil
local humanoid = player.Character:WaitForChild("Humanoid")
humanoid:SetStateEnabled(Enum.HumanoidStateType.Dead, false)

mouse.Move:Connect(function()
	if script.Prevent.Value == false and player.Character ~= nil and workspace:FindFirstChild(player.Name) ~= nil then
		local root = player.Character.HumanoidRootPart
		player.Character:SetPrimaryPartCFrame(CFrame.new(root.Position, (mouse.Hit.p * Vector3.new(1,0,1)) + (root.Position * Vector3.new(0, 1, 0))))
	end
end)

game:GetService("UserInputService").InputBegan:Connect(function(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.Up or inputObject.KeyCode == Enum.KeyCode.Down or inputObject.KeyCode == Enum.KeyCode.Left or inputObject.KeyCode == Enum.KeyCode.Right or inputObject.KeyCode == Enum.KeyCode.A or inputObject.KeyCode == Enum.KeyCode.S or inputObject.KeyCode == Enum.KeyCode.W or inputObject.KeyCode == Enum.KeyCode.D or inputObject.KeyCode == Enum.KeyCode.Thumbstick1 or inputObject.KeyCode == Enum.KeyCode.DPadDown or inputObject.KeyCode == Enum.KeyCode.DPadRight or inputObject.KeyCode == Enum.KeyCode.DPadLeft or inputObject.KeyCode == Enum.KeyCode.DPadUp then
		script.Prevent.Value = true
	end
end)

game:GetService("UserInputService").InputEnded:Connect(function(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.Up or inputObject.KeyCode == Enum.KeyCode.Down or inputObject.KeyCode == Enum.KeyCode.Left or inputObject.KeyCode == Enum.KeyCode.Right or inputObject.KeyCode == Enum.KeyCode.A or inputObject.KeyCode == Enum.KeyCode.S or inputObject.KeyCode == Enum.KeyCode.W or inputObject.KeyCode == Enum.KeyCode.D or inputObject.KeyCode == Enum.KeyCode.Thumbstick1 or inputObject.KeyCode == Enum.KeyCode.DPadDown or inputObject.KeyCode == Enum.KeyCode.DPadRight or inputObject.KeyCode == Enum.KeyCode.DPadLeft or inputObject.KeyCode == Enum.KeyCode.DPadUp then
		script.Prevent.Value = false
	end
end)

Since you are using the Continuous method, this will circumvent the glitching out for that use.

For this example, i added a BoolValue under the LocalScript named "Prevent" (initially set to false)

Whenever the player begins to move (by pressing down any of the control buttons listed above) the value will become false and prevent the mouse function from running, otherwise, when in stand-still, the player will face the mouse.

2 comments

0

it works but it glitches out when you dont move the mouse is there any way to fix that? https://gyazo.com/dfea0f7ed661b0d8e3b9a502116cf73a

0

-1 code too messy

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