Alternatives for Humanoid:MoveTo

I’m currently working on some models, non-player characters and I’m using :MoveTo in order to randomly make them walk at some points. I think that everyone is aware of the current problem developers get when using this function (Humanoid:MoveTo) and I’d like to know if some of you have suggestions, alternatives methods to smoothly move a model in order to replicate some sor of walk.

(Model randomly stops and MoveToFinished no more works after like 7 seconds of timeout)

So far, I’ve tried using BodyMovers such as BodyVelocity and BodyPosition, but the BodyVelocity would always make the model fall for some reason. In this case I could’ve use a BodyGyro, but this gets a bit ridiculous as in a developer should be able to easily and efficiently moving non-player characters. I’ve also looked at using BodyPosition, but it doesn’t really match with the WalkSpeed of the Humanoid. In fact, the speed depends of the dampening and the power (error) and I just don’t see how it correlates with the walkspeed in order to possibly work on a workaround.

Thanks for your suggestions.

In the past I’ve used MoveTo whenever I don’t plan on having a large number of AI as humanoids can be less efficient than other methods. If you are just going to have a few, I personally prefer MoveTo. It needs to be coded in a way that loops and updates itself so that the call doesn’t cancel, but it’s not really anything too crazy.

In situations where I’ve had many NPC’s operating at once and don’t have the resources to be throwing Humanoids around, I typically use a BodyVelocity / BodyGyro combo and calculate the velocity with some high school level physics. If the NPC is still falling when you use BodyVelocity you simply need to raise the amount of velocity that is being applied. Could be an issue of maxForce not being high enough, or one of the other properties. P, I believe, for BodyVelocity.

1 Like

Hmmm. Might as well turn to using BodyVelocity & BodyGyro. Hopefully Roblox fix this problem :+1:

I’d also like to add that this problem only occurs when there’s a player in the game. In fact, it seems to work perfectly in Run Mode.

1 Like

Use Lerp, using physics would be very bad/faulty imo.

you can look it up on the wiki it works vector3 on wiki but idk about cframe but it should be fairly easily to implement customly though

1 Like

Hey guys, I think that I found a workaround for the problem. However, this still relies on the Humanoid.

How does it differs from MoveTo?

Good question. Instead, of using the function Humanoid:MoveTo, I set the WalkToPart and WalkToPoint property in the script itself.

Basically, my idea consists to look goals as if they were directions. First, I tried to set the WalkToPart property to the PrimaryPart of the non-player characters and I would find the direction in which they had to go for reaching the point by doing the following :

local Direction = (Points[i].Position - Model.PrimaryPart.Position).unit*Model.Humanoid.WalkSpeed

I quickly noticed that this was wrong. In fact, the npc would just turn around himself. My solution to solve this first issue consists in creating part that always correspond to the position of the PrimaryPart of the said model. This part is going to be the WalkToPart property of the Humanoid. This is what it looks like when the part isn’t transparent :

image

Yes, there is a non-player character inside of that box.

Keep in mind that this box is always standing on the non-player character, that has been done in a while loop. Also keep in mind that this box will always correspond to the player’s position, not its rotation, so I used .Position and not .CFrame because I don’t want the box to represent any sort of orientation since we’re going to use it in order to determinate the direction in which the player has to go in order to reach the point. I’m going to call this part the referencePart for the sake of the simplicity and let’s just give it a smaller size such as Vector3.new(0.05,0.05,0.05). You can also make it transparent like I did.

Now I’ve covered the WalkToPart, The only thing left is the WalkToPoint property, but that’s quite easy to deal with. In fact, it just corresponds to the line of code I’ve posted up there except that there’s a little change :

local Direction = (Points[i].Position - referencePart.Position).unit*Model.Humanoid.WalkSpeed

The rest consists in doing a simple calculation, the time required in order to reach the part.

local RequiredTime = (Points[i].Position - referencePart.Position).magnitude/Humanoid.WalkSpeed

In addition to that, until the RequiredTime hasn’t been reached, I constantly set the non-player character’s humanoid WalkToPoint property to the current direction it has to take in order to reach the goal and finally, I added a check in order to see if the distance between the goal and the npc is higher than it was at the start. If it’s the case, I just find another path with PathfindingService etc, etc.

That might not be helpful, but here’s some portion of the script that mainly explains my idea behind that. It’s a bit messy, I’m sorry, but it might do it. Note that the referencePart is named ghostPart.

for i = 2,#points do
	if loopAgain == true and private_field.mentalState == "normal" then
		
		local initial_mag = (points[i].Position - model.PrimaryPart.Position).magnitude
		local timeRequired = initial_mag/private_field.humanoid.WalkSpeed
		local inital_t = tick()
							
		local keepWalking = true
		
		private_field.humanoid.WalkToPart = ghostPart
		local direction = (points[i].Position - ghostPart.Position).unit*private_field.humanoid.WalkSpeed
		private_field.humanoid.WalkToPoint = direction
		pointPosition = points[i].Position
		
		spawn(function()
			while keepWalking == true do
				direction = (points[i].Position - ghostPart.Position).unit*private_field.humanoid.WalkSpeed
				private_field.humanoid.WalkToPoint = direction
				wait(.5)
			end
		end)
		
		wait(timeRequired)
		
		keepWalking = false
		
		if (points[i].Position - model.PrimaryPart.Position).magnitude >= initial_mag then
			loopAgain = true
			break
		end
	end
end

If you have any questions, feel free to post them I guess?

6 Likes

I’m about to record/upload some video on YouTube in order to show how that works perfectly :+1:

1 Like
2 Likes