How to Predict Projectile

Hello, I’m wondering how do you predict a projectile’s position? Currently I’m using the formula:

d = st

Example:

local cooldown = 0.5
local speed = 10

local direction = (mousePos - origin).Unit
local lastPos = origin

while true do
   lastPos = lastPos + direction * (speed * cooldown)
   task.wait(cooldown)
end

Yet, for some reason this is behind the actual projectile. Also I’m using a linear velocity inside the projectile. Any ideas?

Thanks.

It could be a server client thing.

But it could also be a task.wait() thibg since it waits the maximum amount of time so it may wait 0.51 seconds instead with an extra 0.1 seconds which build up inacxuracies.

Try using the delta time returned by task.wait instead.

1 Like

That’s a good point after checking the time you can notice a small delay. However, I tried using delta time instead like you said, but I still noticed a small delay am I doing it right?

Script:

local cooldown = 0.5
local speed = 10

local direction = (mousePos - origin).Unit
local lastPos = origin
local lastTime = time()

while true do
	if time() - lasTime >= cooldown then
		lastPos = lastPos + direction * (speed * (cooldown + task.wait()) )
		lastTime = time() + task.wait()
	end
	task.wait()
end

Output:

0.50012229999993 
0.5152939999243245 
0.5147474000696093
0.5162950999801978
0.5182424000231549
0.5149827001150697
0.5167276998981833
0.5163545000832528
0.5161411000881344

Thanks.

What’s the value you’re printing?

I’m printing the time it takes between each loop. For example:

local e = time()
task.wait(0.5)
print(time() - e)

Try using tick() instead of time() idk honestly

Unfortunately this does not work. Thanks for trying though.

It turns out I wasn’t suppoed to add task.wait((), so this is actually more accurate and this ended up being the solutions! However, there is still a small acceptable delay though. I think I can work with that. :smiley:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.