How do i make the character following where the mouse?

  1. What do you want to achieve?
    I want the character keep staring on the cursor, which mean the character would move when the cursor is moving

  2. What is the issue?
    The script didn’t work well, it has some problem. The character just ignoring the cursor

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local torso = char:FindFirstChild("Torso")
local BG = Instance.new("BodyGyro", torso)
local mouse = player:GetMouse()
local RS = game:GetService("RunService")

RS.RenderStepped:Connect(function()
	
	BG.CFrame = CFrame.new(mouse.CFrame.Position, Vector3.new(mouse.CFrame.Position.X,0, 0))

end)

I put the script in a local script in StarterCharacterScripts, Is there anything wrong i missed?

3 Likes
local Run = game:GetService("RunService")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Torso = Character:WaitForChild("Torso")
local Mouse = Player:GetMouse()

local BodyGyro = Instance.new("BodyGyro")
BodyGyro.P = 50000 --Increase/decrease this for more force.
BodyGyro.Parent = Torso

Run.RenderStepped:Connect(function()
	BodyGyro.CFrame = CFrame.lookAt(Torso.Position, Mouse.Hit.Position)
end)
2 Likes

It look like this…
image

I meant it to look like staring at the cursor
Example :
image
To this
image

Edit : Like it just spinning around until it match on the line with the cursor

Oh, I was just fixing up the errors.

1 Like
local Run = 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()

Run.RenderStepped:Connect(function()
	HRP.CFrame = CFrame.lookAt(HRP.CFrame.Position, Vector3.new(Mouse.Hit.Position.X, HRP.CFrame.Position.Y, Mouse.Hit.Position.Z))
end)
2 Likes

It works perfectly for me, Thanks for your help!

No problem, here’s an alternate version which uses a tween for a more fluid motion.

local Tweens = game:GetService("TweenService")
local Run = 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()

Run.RenderStepped:Connect(function()
	local Tween = Tweens:Create(HRP, TweenInfo.new(0.2), {CFrame = CFrame.lookAt(HRP.CFrame.Position, Vector3.new(Mouse.Hit.Position.X, HRP.CFrame.Position.Y, Mouse.Hit.Position.Z))})
	Tween:Play()
end)
1 Like

Don’t know whether this would be more performant vs. lerping. I’d still probably do the latter.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.