Projectile simulation with raycasting

I’ve been working on simulating projectiles using rays while allowing the time it takes to reach maximum distance to be defined. While inaccurate, it does work when using integers but when the time I set goes under ~.6 it doesn’t go any lower. The system I have in place is supposed to be an alternative for wait() but faster. The delta of renderstepped is usually ~.01.

  1. I want to know if having a set travel time for the projectile is even worth doing or if I should just focus on the speed ignoring the time it takes for the projectile to reach its goal.
  2. Is there a way I can create segments faster so that the time it takes to reach the goal is closer to the travel time.

function module.SimulateProjectile(client, mpos) --, maxRayDist, rayLength) 

	local maxProjectileDistance = 200 -- Max distance projectile can travel
	local TravelTime = .2 -- Time it takes for projectile to reach MaxRayDistance
	local RayLength = 5 -- Length of each projectile segment
	local intervalSpeed = (TravelTime/maxProjectileDistance) * RayLength -- Time to wait between projectile segments

	local RayOrigin = client.Player.Character.HumanoidRootPart.Position

	local simProgress = 0

	local currentInterval = intervalSpeed -- Starts at intervalSpeed to make first ray appear immediately
	local projectileAge = 0 -- How much time it took for projectile simulation to be completed

	local connection

	local function fire(delta)

		if simProgress >= maxProjectileDistance then connection:Disconnect() print(projectileAge, delta) return end -- projectileAge is ~.6 while the delta is ~.01 

		if currentInterval == 0 then currentInterval = delta end
		if projectileAge == 0 then projectileAge = delta end

		projectileAge += delta

		if currentInterval >= intervalSpeed then
			currentInterval = 0

			local new = CreateRay(client, mpos, RayOrigin, RayLength) 

			if new == "Ray Intersected" then connection:Disconnect() return end

			RayOrigin = new

			simProgress += RayLength

		else
			currentInterval += delta
		end

	end

	connection = client.RS.RenderStepped:Connect(fire)

end 

Took me a while but updating the size of the ray based on delta is the most accurate way of simulating the projectile.


function module.SimulateProjectile(client, mpos, speed) -- (Client, mouse position, speed of projectile)

	local maxProjectileDistance = 100 -- Max distance projectile can trave
	local RayOrigin = client.Player.Character.HumanoidRootPart.Position
	
	local simProgress = 0
	local t = 0
	local connection
	
	local function fire(delta)
		if simProgress >= maxProjectileDistance then connection:Disconnect() print(t) return end -- t uis the time the simulation took to complete
		
		local RayLength = speed * delta
		simProgress += RayLength
		t += delta
		
		local new = CreateRay(client, mpos, RayOrigin, RayLength) 
		RayOrigin = new
		if new == "Ray Intersected" then connection:Disconnect() return end
		
	end
	
	connection = client.RS.RenderStepped:Connect(fire)
	
end