So i wanted to make a part constantly approach the player at a constant speed, however i dont know how i would achieve that since lerping and tweening dont work, so i dont know how to achieve this.
function chaser.Nav(currentChaser)
chaser.SetSound(currentChaser)
local moveTimes = math.random(800, 900)
for i=1, moveTimes do
if currentChaser.Parent == nil then return end
if not currentChaser.Active.Value then break end
character = player.Character or player.CharacterAdded:Wait()
local moveMulti = 0.0008
if character:GetAttribute("Hiding") == true then moveMulti = 0.008 end
task.wait(moveMulti)
chaser.MoveTo(currentChaser)
end
if currentChaser.Active.Value then
chaser.Despawn(currentChaser)
end
end
function chaser.MoveTo(currentChaser)
character = player.Character or player.CharacterAdded:Wait()
local moveMulti = 0.015
local chaserPos = currentChaser.PrimaryPart.CFrame:Lerp(character.HumanoidRootPart.CFrame, moveMulti)
currentChaser:PivotTo(chaserPos)
local hits = workspace:GetPartsInPart(currentChaser.PrimaryPart)
for i, object in hits do
if object.Parent == character and object.Name == "HumanoidRootPart" then
character.Humanoid.Health -= 70
chaser.Despawn(currentChaser)
end
end
end
This is my current code using :Lerp(), however it causes it so that whenever it gets close to the player the part slows down, which i dont really want.
Keep in mind this is a local script.
3 Likes
you need to use trigonomerty for that…
what your doing is getting distance and using that to do the speed.
Just get the correct rotation vector and then apply a force in that direction
just ask chatgpt if you cant do the math, Most people struggle with it (even I do sometimes)
1 Like
How, exactly, would i start doing something like that? I dont really understand how i could do that.
1 Like
theres a function called Cframe:LookAt() that returns a CFrame from 2 vector3 points.
A looks at point B.
Then (using trigonometry)
local c = 2 --distance you want to go in studs
local Dir = ? --the lookAt() function result goes here
and do some math and boom your done
AGAIN ASK CHATGPT I HATE DOING MATH
The problem with using lerp or tween for this is that you’re effectively going about the equation backwards, even though they technically work. How a lerp works is that it returns a value between value A and B, the value being determined by the second argument. 0 or 1 return the same value as A or B respectively, and any other number is (x/100)% of B starting from A. This post explains it a little better.
Tween is almost the same thing, except it creates a loop bound to framerate that starts at 0 and then ends at 1 across the time given to complete the tween.
Instead, since your concern is speed rather than percentage, you’ll want to use cframes to direct and position the part, like this.
LoopSpeed = 0.05
TravelSpeed = 5 --//Studs per second.
while true do
--//First part of CFrame.new specifies position, and the second argument specifies where it is "looking".
--//Then it is multiplied (how cframes are combined) by the speed to move it forwards.
CloserDirection = CFrame.new(MovePart.Position,Target.Position) * CFrame.new(0,0,-TravelSpeed /LoopSpeed )
--//This keeps rotation
MovePart.Position = CloserDirection.Position
--//Alternatively, this makes the part face the direction it's moving.
MovePart.CFrame = CloserDirection
task.wait(LoopSpeed)
end
You’ll probably also want a magnitude check to make the part teleport to the target directly instead when it’s close enough, otherwise it’d overshoot very slightly.
1 Like