Constant lerp speeds?

So I’m using a lerp to make my bullet have a curved flight. Here is the code

local NewBezier = Bezier.new(p0, p2, p1)
	
for t = 0,1,0.0165 do--0.0165
	local position = NewBezier:CalculatePositionAt(t)
	local derivative = NewBezier:CalculateDerivativeAt(t)
		
	TweenService:Create(bullet, TweenInfo.new(task.wait()), {CFrame = CFrame.new(position, position + derivative)}):Play()
		
	bullet.CFrame = CFrame.new(position, position + derivative)
end

(I’m using this module if your curious Bhristt's Bezier Curve Module (Tween support)

Currently, the bullet will fly faster for slower for shorter shots.
The problem: I want the bullet to go at the same speed no matter how far/close the shot was.

Any replies are appreciated. Thank you!

edit: I relized there was something that I didn’t mean to put in the code but it’s fixed now.

2 Likes

I’m blind and can’t see the position of commas.

The module link appears to not work for me i get this screen

1 Like

Sorry, something was wrong with the link but I fixed it now.

To get a constant time, you need to have the distance and speed unit that you want the bullet to travel.
Using code that would look like this:

local distance = (bullet.Position - yourFinalPosition).Magnitude
speed = 300
local time = (distance/speed)

Where distance is the difference between your origin position and end position.
You also don’t really need a module to have a curved flight path. There is a projectile motion equation you can use that makes it very simple to model.

   local TIME_TO_DESTINATION = 0.5
   local gravityVector = Vector3.new(0, -workspace.Gravity, 0)
   --calculating the velocity needed to reach the destination
   local velocityForDestination = (destinationPosition - originPosition - (gravityVector * TIME_TO_DESTINATION^2)/2)/TIME_TO_DESTINATION

   --Air travel using Cframe
   local airTime = 0
   local connection = nil
   connection = RunService.Heartbeat:Connect(function(dt)
      if airTime < TIME_TO_DESTINATION then
         bullet.CFrame = CFrame.new(originPosition + velocityForDestination * airTime + gravityVector * airTime^2 / 2)
         airTime = airTime + dt
      else
         connection:Disconnect()
      end
   end)

image

2 Likes