I think title says it all, how would I make :MoveTo finish before reaching the desired Position?
What I’m trying to do is make an NPC walk to the HumanoidRootPart of the player but the NPC just goes in the player which is not what I want.
Red line is where it goes and blue is where i want it to go.
I do not know where to even start, I would appreciate it if instead of a script you gave me a method :D! The only one I’ve thought of is the title, but I haven’t found anything about it.
--The current code:
for i,v in pairs (parts) do
if v.Name == "HumanoidRootPart" then
v.Parent.Humanoid:MoveTo(char.HumanoidRootPart.Position)
end
end
well theres multiple ways of going around this. theres two ways that i could think of atm that i would use; one way would be getting the desired position (where the blue point is located on), other way is having some sort of loop after moveto to keep track of the magnitude between the dummy’s position and the red points position.
--In the assumption that everything was running smoothly beforehand
local offset = 2.5 --how far you want the dummy to be apart from character
--Sol 1: Usage of calculating position beforehand
for i,v in pairs (parts) do
if v.Name == "HumanoidRootPart" then
--the issue with this one is that if the character moves you will have a large distance between the two;
local dist = (v.Position - char.HumanoidRootPart.Position).magnitude;
local cf = CFrame.new(v.Position, char.HumanoidRootPart.Position)*CFrame.new(0,0,-dist + offset);
v.Parent.Humanoid:MoveTo(cf.p);
end
end
--Sol 2: Usage of looping till reaching
for i,v in pairs (parts) do
if v.Name == "HumanoidRootPart" then
v.Parent.Humanoid:MoveTo(char.HumanoidRootPart.Position)
--the issue with this one is unless you make a new thread (by using spawn, coroutine, etc) will stop the process if multiple dummies;
while (v.Position - char.HumanoidRootPart.Position).magnitude > offset do wait() end; -- could use runservice instead of wait();
v.Parent.Humanoid:MoveTo(v.Position); --hopefully stops it;
end
end
hopefully i wrote these correctly
of course these arent the only nor the most optimal answers
Thanks for that! First solution works smoothly, I didn’t use second since I’ll be moving more than one humanoid at the same time and it will do one at a time, it’s still brilliant tho :)!
most eficient code, the solution code works but it isn’t eficient.
Distance = 2.5 --Distance from player (destination)
function SmartMoveTo(NPC,Destination,Distance)
NPC.Humanoid:MoveTo(
CFrame.new(
Destination,NPC.HumanoidRootPart.Position
):ToWorldSpace(
CFrame.new(
Vector3.new(0,0,-Distance)
)
).Position
)
end
for i,v in pairs (parts) do --Native code (the code u wrote)
if v.Name == "HumanoidRootPart" then
SmartMoveTo(
v.Parent,
char.HumanoidRootPart.Position,
Distance
)
end
end