What method should I use to move my bullet from point A to B

Magnets have radius which determines if they are attracted to something, you can just do a magnitude check to check if a target is in range and adjust the trajectory of the projectile accordingly.

1 Like

I thought the best way to do guns is by firing a ray to detect if it was hit and just have the bullets be there purely as VFX

1 Like

A solution for simulating the projectile with a realistic ballistic kinematics approach:

Equation for finding Position (p1) given initial position (p0), initial velocity (v0), time (t), and constant acceleration (a) in kinematics:
p1 = (p0) + (v0)(t) + (1/2)(a)(t^2)

For a normal ballistic projectile, the way I do it is:

At inception:

  1. Record start time
  2. Record start position
  3. Record start direction
  4. Record starting velocity

On heartbeat:

  1. Get time in flight (current time - start time)
  2. Check if total time in flight is greater than some max value to avoid projectiles living forever and never hitting anything
  3. Plug initial velocity, initial position, gravity, and time in flight into the kinematics equation to solve for current position
  4. Shapecast from last position to new position to find obstacles
  5. If obstacle, apply damage and destroy projectile. If no obstacle, continue.
  6. Update the position of the projectile to the new position
  7. Save the new position as ā€œlastā€ position for the next heartbeat to compare

This assumes a constant acceleration (gravity) is the only force acting on it. But if you want ā€œaimbotā€, e.g. curve projectiles toward nearby targets, then youā€™ll need to introduce another force acting on it. A couple changes would need to happen:

Since the path isnā€™t pre-determined anymore, you canā€™t use the position and velocity at inception for the whole flight anymore. Instead, use the previous frameā€™s velocity and position as the new v0/p0, and use time since last frame instead of total time in flight for t.

You still have a constant gravity, but in order to introduce a curve toward enemies, you can find the closest enemy and add another acceleration force in the direction of that enemy, scaled exponentially by how close the distance to that enemy is (e.g. the closer the enemy, the stronger the curve force).

The strength of this ā€œcurveā€ force (F) on the projectile can be imagined as a gravitational force (G) acting on the Mass of the character (M1) and the Mass of the projectile (M2) scaled by the Distance between the two (D). Plug these variables into the equation for gravitational force:
F = (G Ɨ M1 Ɨ M2) / (D^2)

In order to achieve a fun ā€œcurveā€, youā€™d probably want a pretty high G value in this case rather than using the normal gravity constant. You can make up values for M1 and M2, or omit them entirely if you donā€™t care about simulating different curves based on different targets.

So if you assume every enemy and projectile has a constant mass, just use F = G / D^2 where changing G is the way that you tune the strength of the curve.

2 Likes

This is so incredibly phenomenally smart that my two brain cells have a difficult time even comprehending it :face_with_peeking_eye:

It might take me some time to work through this but luckily my game is a top down shooter so I can just forget the Y axises entirely, and along with it, gravity!

TY


I made a 3d prototype and this is way too cool haha

1 Like

DUDE, itā€™s like you pulled the idea straight from my brain!

Would it be cool if you uncopylocked it so I can look around and see how you did it and make it 2D?
If not thatā€™s totally cool

ProjectileAimbot.rbxl (79.9 KB)

Hereā€™s the file.

Disclaimer

This file is provided as-is for learning purposes only and not for commercial use; that is, please do not re-use my 2d or 3d assets or directly copy/paste my code into a project that will earn any money. But you may take any learned concepts from reading it and write your own code.

Itā€™s pretty over-engineered to be useful as a reference, and you probably shouldnā€™t use OOP for a projectile because of the metatable overhead, but for experimental purposes this is fine.

One improvement would be in the target picking algorithm. Instead of picking the closest target, pick the target that is closest on the forward path of the projectile.

1 Like

Where did your learn to code?

You are extremely good at coding and game dev in general

Thank you. Most of my learning happened through years of developing on roblox, but I also went to university for it later.

1 Like

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