How to fix my NPC position prediction

So my script is working fine but it still misses.

local function PredictNextPosition(targetPosition, targetVelocity)
	if targetPosition ~= nil and targetVelocity ~= nil then
		local duration = 1
		local gravity = workspace.Gravity
		local targetPos = targetPosition + targetVelocity
		local direction = targetPos - firePoint.WorldPosition
		local height = gravity * duration * 0.5
		local force = direction / duration + Vector3.new(0, height, 0)
		local endPosition = targetPos * duration
		return force, endPosition
	end
end

Anything I am doing wrong here?

1 Like

I’m no mathematician, but you might need to use math.log for calculating the height.

1 Like
local height = math.log(gravity * duration * 0.5)

Shoots too low.

1 Like

You might need to multiply (or divide) it by math.pi if I’m not mistaken. I had to make an arc system before, but I used math.cos and not math.log (now I believe log is better).

1 Like

What number would be in math.pi?

1 Like

math.pi is a variable, not a function. You don’t pass a number parameter to it.

I tried multiplying and dividing and it still doesn’t reach the target.

local height = math.log(gravity * duration * 0.5) / math.pi
local height = math.log(gravity * duration * 0.5) * math.pi
1 Like

I’m not sure then. Maybe you should try math.cosh or math.cos.

They need a number in them, right?

1 Like

Yes, they do. You might need to use a combination of it with math.sin or math.sinh.

Let me create a place file for you to test it.

Okay. Once it’s sent, I’ll notify you when I’m done experimenting.

1 Like

Baseplate.rbxl (65.6 KB)
This is created using FastCast.
If you change the ProjectileSpeed, it won’t get to the target.

After testing it, I think I fixed it. Here’s the updated function:

local function PredictNextPosition(targetPosition, targetVelocity)
	if targetPosition ~= nil and targetVelocity ~= nil then
		local targetPos = targetPosition + (targetVelocity*projectileDuration)
		local direction = targetPos - part.Position
		local height = gravity * projectileDuration * .5
		local force = direction / projectileDuration + Vector3.new(0, height, 0)
		local endPosition = targetPos * projectileDuration
		return force, endPosition
	end
end

You just forgot to multiply it by projectileDuration.

1 Like

It still doesn’t work. When you change the ProjectileDuration to something else, it won’t hit the target.

1 Like

Never mind. I probably did something wrong. It really works!

1 Like

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