Having trouble accounting for bullet drop when predicting where to shoot

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    I want to predict where shoot a moving target while accounting for drop.

  2. What is the issue?
    I have a script calculating where to shoot to hit a moving target and at the moment it works perfectly fine, until I put bullet drop into the equation. I already have a fairly good idea why this is. Instead of dropping my bullet down x studs every renderstep, I just move the direction of the line the bullet travels on downwards x studs. This causes the shoot position to be off, as my equation for adding drop is wrong.

    Without drop:
    https://gyazo.com/89f30ae5434cb57e058b1072abfd852f

    With drop:
    https://gyazo.com/b08aae95aa600c265250982dd0346615

  3. What solutions have you tried so far?

    Really, I am pretty clueless with this one, as I can not construct an equation that represents how many studs the bullet is doing to drop over the amount of time it takes to reach the target. I have tried reworking the equation many times over, but to no avail.

  4. Additional Resources

    The way my bullets travel:

	local drop = Vector3.new(0, (stats.Drop / 100) * margin, 0) --stats is just a modulescript with gun statistics, in the gif the stats.drop was .2
    local aim = (CFrame.new(start.Position, target) * CFrame.Angles(math.rad(spreadx), math.rad(spready), 0)).LookVector --spread was off for the test of course

    game:GetService("RunService").RenderStepped:Connect(function(last)
        local margin = last * 60
        local pos = aim * (margin * Velocity)

        bullet.CFrame = CFrame.new(bullet.Position + pos, (bullet.Position + pos) + pos.Unit) 
        aim = aim - drop --ignore the 10000 parentheses, its just an organization problem
    end)

    --this was just snippets put together of the actual script i made, but should be all you need
    How I calculate where to shoot:
    local Target = thing:WaitForChild("Target") --what its supposed to hit
    local Marker = Target:Clone() --where to shoot
    local Shooter = thing:WaitForChild("shooter") --where it shoots from

    Marker.Color = Color3.fromRGB(255, 0, 0)
    Marker.Name = "Marker"
    Marker.Parent = thing

    local bulletStart = Shooter.Position
    local bulletspeed = 2
    local drop = .2

    local last = Target.Position

    Target:GetPropertyChangedSignal("Position"):Connect(function()
	     local speed = (Target.Position - last).Magnitude
         local direction = (Target.Position - last).Unit
         local offset = (bulletStart - Target.Position).Magnitude
	     local timetotake = offset / bulletspeed
	
	     local dropoffset = Vector3.new(0, (drop * timetotake), 0) --wrong

	     Marker.Position = (Target.Position + (direction * (speed * timetotake))) + dropoffset
		
	    last = Target.Position
end)

Any help is greatly appreciated.

2 Likes

I have the same issue, but can’t figure out the solution. Can anyone help?

Solved this a bit ago, the equation for solving the aim on a moving target with projectile motion ended up being a quartic. Read a couple of blog posts that explained the problem and had to port some quartic solving C code to lua iirc. Ended up working perfectly and will for you as long as you read these posts thoroughly:

(Use fixed speed with a moving target for projectile motion)

P.S.
The equations used in the original post for modeling the projectile’s motion were not accurate to real projectile motion because I didn’t even know there was such a thing yet. If you do not use the standard drop = 1/2gt^2 then this will not work for you.

4 Likes