How To Make Your Character Follow Your Mouse For Magic

Hello! I’m trying to make my character follow my mouse when I’m holding a input for my anime magic ability I wanna ask to the people who knows how to do magic on how you guys do it, I tried to use an inf loop then break it after it’s done but it’s really laggy I tried to use runservice but I don’t know how to stop it. I tried to look in the forum but I can’t find anything that can relate to my problem.

An example code that I did that was laggy

while wait() do
    if debounce == 2 then
        player.Character.HumanoidRootPart.CFrame = CFrame.new(plr.Character.HumanoidRootPart.Position, Mouse.Hit.Position)
		player.Character.HumanoidRootPart.Anchored = true
    else
        break
    end
end
1 Like

There are plenty of free models that do this for you, when in doubt don’t be afraid to look in the Toolbox. It’s there for a reason :slight_smile:

1 Like

Use CFrame.lookAt to make the HumanoidRootPart point to Mouse.Hit.Position in a loop.

local run = game:GetService"RunService"
local players = game:GetService"Players"
local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild"Humanoid"
if not humanoid.RootPart then
	humanoid:GetPropertyChangedSignal"RootPart":Wait()
end
local root = humanoid.RootPart

local mouse = player:GetMouse()

local function onRenderStep()
	root.CFrame = CFrame.lookAt(root.Position, Vector3.new(mouse.Hit.Position.X, root.Position.Y, mouse.Hit.Position.Z))
end

run.RenderStepped:Connect(onRenderStep)

You could implement a tween with a duration of 0.1-0.2 seconds for a more fluid animation.

2 Likes