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.
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
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:
- Record start time
- Record start position
- Record start direction
- Record starting velocity
On heartbeat:
- Get time in flight (current time - start time)
- Check if total time in flight is greater than some max value to avoid projectiles living forever and never hitting anything
- Plug initial velocity, initial position, gravity, and time in flight into the kinematics equation to solve for current position
- Shapecast from last position to new position to find obstacles
- If obstacle, apply damage and destroy projectile. If no obstacle, continue.
- Update the position of the projectile to the new position
- 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.
This is so incredibly phenomenally smart that my two brain cells have a difficult time even comprehending it
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
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.
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.