How to make my character face to where the mouse is?

Hello! So, I’m trying to make an game with a RPG top-down camera view. I’m currently working on the aim aspect of the game where the character will face to the direction of where the mouse is.

I mean like this: image

I was thinking of making the HumanoidRootPart face mouse.Hit but I don’t really know how to implement it. Help or tips are greatly appreciated.

10 Likes

Here are some resources to solution of people who have answered this in the past.

1 Like

I found this bits of script which actually work (kinda?).

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local rootPart = character:WaitForChild("HumanoidRootPart")

local mouse = player:GetMouse()

mouse.Move:Connect(function()
	rootPart.CFrame = CFrame.new(rootPart.Position, rootPart.Position + mouse.Hit.p)
end)

However, it is extremely buggy and is almost unplayable. Is there any more efficient way of implementing such system?

2 Likes

You can add a debounce or you can try this

local RunService = game:GetService("RunService")

local Players = game:GetService("Players")

local Player = Players.LocalPlayer

local Character = Player.Character or Player.CharacterAdded:Wait()

local HRP = Character:WaitForChild("HumanoidRootPart")

local Mouse = Player:GetMouse()

RunService.Heartbeat:Connect(function()

HRP.CFrame = CFrame.new(HRP.Position, HRP.Position + Vector3.new(Mouse.Hit.lookVector.x, HRP.CFrame.lookVector.y, Mouse.Hit.lookVector.z))

end)

this method should be more performance-friendly

Added some variables and an if statement to keep it from setting HRP Cframe when the player hasn’t moved

local RunService = game:GetService("RunService")

local Players = game:GetService("Players")

local Player = Players.LocalPlayer

local Character = Player.Character or Player.CharacterAdded:Wait()

local HRP = Character:WaitForChild("HumanoidRootPart")

local Mouse = Player:GetMouse()

local OldCFrame = nil

RunService.Heartbeat:Connect(function()

local NewCFrame = CFrame.new(HRP.Position, HRP.Position + Vector3.new(Mouse.Hit.lookVector.x, HRP.CFrame.lookVector.y, Mouse.Hit.lookVector.z))

if OldCFrame ~= NewCFrame then

HRP.CFrame = CFrame.new(HRP.Position, HRP.Position + Vector3.new(Mouse.Hit.lookVector.x, HRP.CFrame.lookVector.y, Mouse.Hit.lookVector.z))

OldCFrame = HRP.CFrame

end

end)
2 Likes

You’re gonna have to modify to cframe of this script to mouse instead of camera

local Gyro = Instance.new("BodyGyro",player.Character.HumanoidRootPart)
player.Character.Humanoid.AutoRotate = false



game["Run Service"].RenderStepped:Connect(function()
		local Root = player.Character.HumanoidRootPart
		Gyro.MaxTorque = Vector3.new(math.huge,math.huge,math.huge)
		Gyro.D = 100
		Gyro.P = 10000
		Gyro.CFrame = CFrame.new(Root.Position, Root.Position + Vector3.new(cammer.CFrame.LookVector.X,0,cammer.CFrame.LookVector.Z))
		wait()
end)
2 Likes

You’re close!

I’m assuming what you meant by ‘buggy’ is that the humanoid is trying to tilt to align with the mouse’s hit position while the humanoid is refusing at the same time due to built-in BodyGyro.

I’d make some slight changes to the code:

local Player = game.Players.LocalPlayer
local Character = Player.Character
local Root = Character.HumanoidRootPart

local Mouse = Player:GetMouse()
local RunService = game:GetService("RunService")

RunService.RenderStepped:Connect(function()
	local RootPos, MousePos = Root.Position, Mouse.Hit.Position
	Root.CFrame = CFrame.new(RootPos, Vector3.new(MousePos.X, RootPos.Y, MousePos.Z))
end)
35 Likes

This works so smooth! Thanks man!

7 Likes