How to move a part towards another at a constant pace

I want to have my part (VoicesPart) slowly advance towards the player at a constant speed. I dont want to use TweenService because the player might move while this is happening. How can I do this?

I have looked around the forums but mostly only found things for humanoids. I am using just a part, not a humanoid.

This should be escapable btw

4 Likes

Hi,
I think a good way to accomplish this would be to use :Lerp()
You can use this and setup a function to iterate from 0 to 1 with a delay.

Example

local function goToTarget(part: BasePart, target: BasePart, increment: number)

	-- part should be the part you want to move
	-- target should be a part on the player
	
	local startCFrame = part.CFrame

	for percent=0, 1, increment do
		part.CFrame = startCFrame:Lerp(target.CFrame,percent)
		task.wait()
	end

end

Notes / Comments

You will have to tweak the “increment” and “task.wait()” a lot to get it to move smoothly toward the player though.

1 Like

When I spawn it it doesnt move. What did I do wrong?

goToTarget(script.Parent,hrp,5)
local function goToTarget(part:BasePart,target:BasePart,increment:number)
	-- part should be the part you want to move
	-- target should be a part on the player

	local startCFrame = part.CFrame

	for percent=0, 1, increment do
		part.CFrame = startCFrame:Lerp(target.CFrame,percent)
		task.wait()
	end
end
1 Like

You have to tell the script where to find the items in the script named ‘part’ and ‘target’.

for example, before or during the function you have to declare what part is.
part = script.Parent
or
part = workspace.SoundPart

The percent is going from 0 to 1.
So I suggest setting the increment (which is currently 5) to something along the lines of “0.05”

goToTarget(scripts.Parent,hrp,0.05)
1 Like

The issue is is that this isnt escapable as it will eventually catch up to you no matter what.

Current code:

goToTarget(script.Parent,hrp,0.1)
local function goToTarget(part:BasePart,target:BasePart,increment:number)
	-- part should be the part you want to move
	-- target should be a part on the player

	local startCFrame = part.CFrame

	
	for percent = 0, 1, increment do
		print(percent)
		print(increment)
		part.CFrame = startCFrame:Lerp(target.CFrame,percent)
		print(startCFrame:Lerp(target.CFrame,percent))
		task.wait(1)
	end
end
1 Like

From my understanding you are wanting an enemy that you can outrun?

If so, you would need to setup a much more advanced AI that will path to the player and when a certain distance requirement is met it will stop following them.

2 Likes

it might be because the for loop doesnt go on forever, it only repeats the amount of times relative to your alpha, so 0.1, means it does 10 loops, the reason the for loop works like that is because thats how the lerp function works, if you want him to teleport like he is now though, its best to just pivot it every second

1 Like

Well how this works is that it will always be coming for you but you can avoid it. It will never loose you.

1 Like

it wouldnt be hard, he can just use the same code, but make it slower

1 Like

The issue is that I want this enemy to be able to be outrun but also never loose sight of the player. Kind of like a nextbot that can see through walls and pass through them.

1 Like

so basically its always going to the player but to where it can be outrun, then it will still work, just make it slow

1 Like

I think what they are saying is since lerp uses a percentage it will always stay the same amount away from the player even if they move.
They may be wanting it to move at a constant velocity towards the player though.

2 Likes

How the script works is that it increments a percent which is the percentage of how far away the enemy is from the player. This will always eventually become 1 but thats not what I want, I want it to always go twards the player but never become 1 if they outrun it.

(me and @eeeeoklol had the same idea lol)

1 Like

ohh right, i forgot lerp uses smooth movement

1 Like

Have you tried this?

local runService = game:GetService("RunService")

local function goToTarget(part: BasePart, target: BasePart, increment: number)

	-- part should be the part you want to move
	-- target should be a part on the player
	
	local startCFrame = part.CFrame
    local speed = 50 -- Feel free to change

	for i = 0, 1, increment do
        local progress = (i / speed)
		part.CFrame = startCFrame:Lerp(target.CFrame, progress)

		runService.Heartbeat:Wait()
	end
end
2 Likes

could putting a humanoid inside the model, and just using :MoveTo work, im not sure because i dont mess around with humanoids too much

1 Like

Its definitely easier with a humanoid but they said before they didn’t want to use one.

@kirb_cat you will have to mess around with CFrames to make it always lookAt() the player then move forward at a constant speed by multiplying the lookVector.

1 Like

The part is anchored also the part wont always be on solid ground, causing it to fall into the void.

A demo of where it would spawn:

That might just work. Ill see if I can do that.

1 Like