You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve? Keep it simple and clear!
The player in my game follows a Humanoid:MoveTo(goal). I want it so when the player walks towards it, he can not walk backwards, only forward and to the sides. -
What is the issue? Include screenshots / videos if possible!
The player is in the MoveTo() loop, but he can just walk backwards and I don’t want that. -
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
Everything I could think of, and I’ve searched for hours on devforums, discord servers, etc.
Here is the code that changes movement, I have another script that makes the player moves toward a part when a button is pressed. That script is just a simple Humanoid:MoveTo(goal.Position)
and btw the function is being called always and the moving sideways works perfectly, it’s just the forward and backwards that don’t work together with the MoveTo()
-- local function Move()
local UserInput = UserInputService:GetFocusedTextBox()
if UserInput then return end -- If typing, etc.
local cameraForward = workspace.CurrentCamera.CFrame.LookVector
local movingDirection = humanoid.MoveDirection
local dotProduct = cameraForward:Dot(movingDirection)
local rightVector = game.Workspace.CurrentCamera.CFrame.RightVector
if dotProduct < -0.5 then -- player tries to move backward
humanoid:Move(Vector3.new(0, 0, 0), false)
elseif dotProduct > 0.5 then -- player tries to move forward
humanoid:Move(Vector3.new(0, 0, 0), false)
elseif UserInputService:IsKeyDown(leftKey) then
local adjustedVector = CFrame.new(Vector3.new(), rightVector) * CFrame.Angles(0, math.rad(-30), 0)
humanoid:Move(-adjustedVector.LookVector * speed, false)
elseif UserInputService:IsKeyDown(rightKey) then
local adjustedVector = CFrame.new(Vector3.new(), rightVector) * CFrame.Angles(0, math.rad(30), 0)
humanoid:Move(adjustedVector.LookVector * speed, false)
else
humanoid:Move(Vector3.new(0, 0, 0), false)
end
end