Help with Projectiles

Hey, I’m working on a little raycast-projectile system and whilst I can do equations in class on projectiles I’m pretty limited with implementing them into luau. I’ve done this so far:

connections["renderStepped"] = run_service.PreRender:Connect(function(delta_time)
	
	local projectile_origin = caster.Position
	local projectile_direction = (final.Position - projectile_origin).Unit * 0.55 * cast_duration --* 0.28
	local acceleration = Vector3.new(0, -workspace.Gravity / 2, 0) * 0.28
	
	
	
	local projectile_displacement = Vector3.new(
		(projectile_origin.X + projectile_direction.X + ((acceleration.X * (cast_duration^2)) / 2)), 
		(projectile_origin.Y + projectile_direction.Y + ((acceleration.Y * (cast_duration^2)) / 2)), 
		(projectile_origin.Z + projectile_direction.Z + ((acceleration.Z * (cast_duration^2)) / 2))
	)

	
	local ray_result = workspace:Raycast(projectile_origin, projectile_displacement - projectile_origin, params)
	projectile_origin += projectile_displacement

	
	if ray_result then
		
		print("Hit")

		projectile.CFrame = CFrame.new(ray_result.Position)

		
		connections["renderStepped"]:Disconnect()
		connections["renderStepped"] = nil
		
	else
		
		
		projectile.CFrame = CFrame.new(projectile_origin)
		projectile.CFrame *= CFrame.Angles(0, math.rad(math.pi / 2), 0)

		cast_duration += delta_time
		
		
	end
	
	
	if cast_duration >= 55 then
		
		print("Cast terminated")
		connections["renderStepped"]:Disconnect()
		connections["renderStepped"] = nil
		
	end
	
end)

I used s = vt for projectile_distance & a SUVAT equation of motion, s = ut + 1/2at^2 for displacement adding on the current position to the equation to make s = s0 + ut + 1/2at^2.

My issue is that even though I’ve provided a path for it to go to on the x axis, it doesn’t go that way only moving on the Y axis. It also prematurely terminates itself and ends the cast as if it’s hit something but it hasn’t. It might be something small and silly but if anyone could give me a hand it would be greatly appreciated. :smiley:

A couple of things I noticed for clarity:

The first declaration of projectile_displacement is not actually displacement. Instead, it’s the projectile’s end position. If you want it to only be the displacement, then remove the projectile_origin from each equation.

ray_result is raycasting from the projectile_origin with a direction of projectile_displacement - projectile_origin. If you make the aforementioned fix, then the new direction should just be projectile_displacement.

local projectile_direction = (final.Position - projectile_origin).Unit * 0.55 * cast_duration

The calculation performed in this line seems to be using cast_duration when delta_time would make more sense, but ultimately this depends on the intended behavior for the aforementioned clarity and paragraph below.

Otherwise, I can’t think of much that is causing the premature termination. More debug info would help - what are caster and final? Is cast_duration modified anywhere (other than initial declaration) outside of the provided code snippet?

1 Like

Hi,

Thanks for the first few things.

cast_duration is basically delta_time but it starts off at 0,caster and final are just parts in the workspace.

There’s a new issue though, the cast is starting underneath the baseplate for some reason and also it still doesn’t move in the direction applied.

Here it is from the projectile’s perspective:

Hey, you found any solutions? Thanks