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.
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.
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
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
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.
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
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.
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.
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.
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
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.