Raycast Projectile Issue, Projectile Speed Changes Based on FPS

I want to make a raycast projectile system, like fastcast (please don’t send me a link to fastcast as a solution). However, the projectiles speed changes based on framerate and I can’t really find a fix for it. Here is an example:

As you can see, the projectiles speed gets slower when the fps is lower, and faster when the fps is higher. If anyone knows how to fix this with some deltatime filtering or something like that, please reply. Thank you. Here is the code by the way if you want to examine it yourself:

--start is the origin vector3 and direction is a cframe lookvector

function createProjectile(start, direction, params, bullet)
	local maxdist = 1000
	local stepDistance = 1
	
	local newStart = start
	local newdir = direction * stepDistance

	local defaultBulletClone = bullet:Clone() or defaultBullet:Clone()
	
	defaultBulletClone.CFrame = CFrame.lookAt(newStart, newStart - newdir)
	defaultBulletClone.Parent = workspace.Terrain
	
	runService.Heartbeat:Connect(function(dt)
		defaultBulletClone.CFrame = CFrame.lookAt(newStart, newStart - newdir)

		local result = workspace:Raycast(newStart, newdir, params)
		
		newdir -= Vector3.new(0, 0.0025, 0)
		newStart = newStart + newdir
		
		if result then
			defaultBulletClone:Destroy()
			return result
		end
	end)
end

well its cause you are using run service heartbeat which is dependent on the frame rate, as for a solution I think a while loop might work

Or you could just get the ratio between the frame rate and the movement increment and apply it to the code

I’m pretty sure that’s the best option since using task.wait() or wait() can slow down depending on fps

I think this might work

newdir -= Vector3.new(0, 0.0025 * dt, 0)

might need some tweaking to make it match

I’ve figured it out by setting a limit to the frequency it updates, thank you though.

1 Like

However, I’m gonna have to find a way to make the renderstep function return the result and return that in the createprojectile function.

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