How can I make my character go toward my mouse?

Hello,
I’m trying to make my character go toward my mouse, but for some reason it keeps going the same direction how can I fix this? Any help is appreciated.
Code:

local player = game.Players.LocalPlayer

local mouse = player:GetMouse()

local RunService = game:GetService("RunService")

local ContextActionService = game:GetService("ContextActionService")

local moveValue = 0

--Move

local function onMove(actionName, inputState)

if inputState == Enum.UserInputState.Begin then

moveValue = -100

elseif inputState == Enum.UserInputState.End then

moveValue = 0

end

end

--Update

local function onUpdate()

if player.Character and player.Character:FindFirstChild("Humanoid") then

local MoveDirection = moveValue

player.Character.Humanoid:Move(Vector3.new(0,0,MoveDirection), false)

end

end

--Mouse

local function onAim()

if player.Character then

local rootPart = player.Character:FindFirstChild("HumanoidRootPart")

local mouseLocation = Vector3.new(mouse.Hit.X, rootPart.Position.Y, mouse.Hit.Z)

rootPart.CFrame = CFrame.new(rootPart.Position, mouseLocation)

end

end

-- Set up control bindings

RunService:BindToRenderStep("Control", Enum.RenderPriority.Input.Value, onUpdate)

ContextActionService:BindAction("Aim", onAim, false, Enum.UserInputType.MouseMovement)

ContextActionService:BindAction("Move", onMove, true, "w", Enum.KeyCode.Up, Enum.KeyCode.DPadUp)

Thank you in advance

2 Likes
local function onUpdate()

if player.Character and player.Character:FindFirstChild("Humanoid") then

local MoveDirection = moveValue

player.Character.Humanoid:Move(Vector3.new(0,0,MoveDirection), false)

end

end

I believe it is because here, you are not adding the vector3 value onto the character’s current position, rather making it always walk towards 0,0,-100

1 Like

Can you give me an example please?

1 Like

Here:

local function onUpdate()

if player.Character and player.Character:FindFirstChild("Humanoid") then

local MoveDirection = moveValue

player.Character.Humanoid:Move(Vector3.new(player.Character.PrimaryPart.X,player.Character.PrimaryPart.Y,MoveDirection), false)

end

end
1 Like
local players = game:GetService("Players")
local player = players.LocalPlayer or players.PlayerAdded:wait()
local character = player.Character or player.CharacterAdded:wait()
local humanoid = character:WaitForChild("Humanoid")
local mouse = player:GetMouse()
local mousePos = mouse.Hit

mouse.Button1Down:Connect(function()
	local mousePos = mouse.Hit.p
	humanoid:WalkToPoint(mousePos)
end)

Simple script which will attempt to make the character walk to a point when they click somewhere on the screen.

Place inside the StarterCharacterScripts folder.

1 Like

The code there should move the character on it’s Z value (MoveDirection) relative to it’s own current position

After doing some testing I got an error do you know what’s wrong?
RunService:fireRenderStepEarlyFunctions unexpected error while invoking callback: X is not a valid member of Part "Workspace.SilentSuprion.HumanoidRootPart"

1 Like

Sorry, i made some mistakes. I had not experimented with retrieving specific X,Y,Z values from a position property beforehand.

local function onUpdate()

if player.Character and player.Character:FindFirstChild("Humanoid") then

local MoveDirection = moveValue

player.Character.Humanoid:Move(player.Charater.PrimaryPart.Position + Vector3.new(0,0,MoveDirection), false)

end

end

It still has an error, but no need for saying sorry it’s not your fault I’m confused too. The output is:
RunService:fireRenderStepEarlyFunctions unexpected error while invoking callback: Charater is not a valid member of Player "Players.SilentSuprion"

Ah, a simple typo. Change Charater to Character.

Oh sorry for not noticing I just copied and paste since I’m in a rush lol.

Rip doesn’t seem to work. Thank you for trying.

Oh. Does it still go to the same direction?

Sorry I can’t tell, but I think it’s going diagonal instead of a straight line.

I recommend using UserInputService of you do not need to unbind the event as the bind events get messy.

In Fact a much more simpler method would be to check every frame. I don’t think there is a performance drop.

Also the thing to use is IsMouseButtonDown()

Finally PosB + PosA = VectorAB
That is the rule to get a vector between 2 points. Then we can extract that vector.

function Stepped()
    if UserInputService:IsMouseButtonDown(LMB) then -- LMB as variable
        Humanoid.MoveDirection = (Mouse.Hit.Position - Torso.Position) * Vector3.new(1,0,1)).Unit -- Flooring and Normalizing are unnecessary but I do it here for good practice.
    end
end

Additionally check if Vector is NaN in the edge case where Mouse.Hit.Position is nil or is equivalent to Torse.Position

I tried using UserInputService, but it had some errors, but can you further explain how unbinding controls work? I only want to go forward.

--parent to starterplayerscripts
local Event

Event = game:GetService("RunService").RenderStepped:Connect(function()
	if game.Players.LocalPlayer.Character then
		game.Players.LocalPlayer.Character:MoveTo(game.Players.LocalPlayer:GetMouse().Hit.p)
	end
end)

--Event:Disconnect()

just make sure you don’t hover your mouse over the void or you will p e r i s h

I’m sorry for the confusion, but what I want is the character to go toward the mouse at a constant speed. Right now I’m exploring unbinding actions, but I’m stuck right now.

What does going to the mouse mean? Literally flying? Forcing the character to walk to it? Please be more clear.

In my description I meant toward my mouse like the direction of the mouse. Not directly to the mouse position just the angle.