I am trying to make and fps game and i need to add bullet drop, travel time and bullet spread. how would i do this? i am kinda new to fps development and have no idea how 75% of the math.
functions such as math.sin
etc work
The way I would do drop is to set a speed (this varies of course).
Once you have set the Speed you would fire an event which tweens the bullet.
Make the script look at how many studs the bullet have travelled and then convert those studs to meters (use this link if stuck : How many studs is there in a meter)
Then, set a loop so for every 5 meters (for example), the bullet would drop by 1.5 studs.
That’s a basic roundup on the basic principles which need to apply. The rest however, is yet to be discovered by myself.
no idea what most of that meant, so would i have to make a physical part for the bullet or keep using just a ray?
I would personally use a part however using a ray is just up to preference. I’ll edit the post into smaller chunks.
how would i do it with a ray? would i just drop the y
of the target by the amount?
Look at this:
x = distance travelled
y = 1.5 (for example)
For every 10 studs x has done, a script would modify the y co-ordinate by -y.
That’s what I mean by travelling.
You can simulate spread by ‘rotating’ the direction of the weapon ray. This will mean that there will be much more spread from further away, while weapons will be more accurate the closer they are to the target.
- Set up a ‘spread’ value for your weapon. For example, my AK47 has a spread of 2.
When the player fires their gun:
-
Generate two random number ranging from -spread to spread; one for ‘x’ and one for ‘y’. The best way to do this is using the Random datatype
-
Generate a starting CFrame, with the weapon’s muzzle as its position, which faces the mouse’s position. You can do this easily using the CFrame.new(position, look_towards) constructor. This CFrame will only be used for the direction of the ray.
-
Rotate the target CFrame by your random ‘x’ and random ‘y’ values, in radians.
-
Now you can generate the ray for the weapon, which now has spread added to it. The direction of the ray you want to cast is the LookVector of the CFrame you constructed; this is simply a CFrame that faces the mouse’s position, but with rotation to emulate spread.
-
Construct a new ray. The origin of the ray should be the actual muzzle position of the gun; i.e. where bullets originate from. The direction of the ray should be the LookVector above, multiplied by the range of the weapon (e.g. 500 studs). This is because the LookVector is a unit vector with a magnitude of 1, so multiplying it by the weapon range, 500, creates a ray that starts from the muzzle, and projects in the direction of the mouse (with spread) for 500 studs only.
Here is an example of my code.
local muzzle_pos = weapon_muzzle.Position
local spread = weapon_data.Spread
local random_generator = Random.new()
local spread_x = random_generator:NextNumber(-spread, spread)
local spread_y = random_generator:NextNumber(-spread, spread)
local target_cframe = CFrame.new(muzzle_pos, Mouse.Hit)
local spread_rotation = CFrame.fromEulerAnglesXYZ(
math.rad(spread_x),
math.rad(spread_y),
0
)
target_cframe = target_cframe * spread_rotation
local direction = target_cframe.LookVector
local ray = Ray.new(
muzzle_pos,
direction * weapon_data.Range
)
Further resources
-
Battle Royale – good open sourced shooter for learning.
-
Making a raycasting gun - very very simple tutorial, which you could use to clarify any confusions about raycasting.
-
[Understanding CFrames](CFrames | Documentation - Roblox Creator Hub
still trying to work out how to make bullet drop. how would i do that with a ray?
A ray is based on a orgin and direction. Simply change the final direction to be a few studs down depending on the distance traveled.
Look at some basic mechanics (physics). I would do this by defining a horizontal muzzle velocity for the bullet. From that, you can calculate the bullets flight time according to the distance to the target. Then you can multiply this flight time by acceleration from gravity to get the change in the bullet’s vertical velocity. Plug this into a kinematics equation to find vertical displacement. You can then decrease the Y-axis of the Mouse.Hit position you’re using as the “target position” by said displacement to emulate bullet drop from gravity.
If this doesn’t make sense, I’ll type up a basic example in code. It should be fairly straightforward physics
if i could post gifs i would post the ??? gif. i have no idea what you said
Essentially you are going to need to create multiple rays. This is because a bullet moves in a parabolic fashion. Since a ray is a straight line, you need multiple straight lines to create a curve.
You can use https://www.khanacademy.org/science/physics/two-dimensional-motion to learn about projectile motion. When you are in middle-school/high-school (or equivalent in your country), your
Physics 101 mechanics class will cover projectile motion most likely.
Essentially, you have an equation y=-x^2 + b; this is kinda like y=mx+b which you learn in Algebra 1. Your bullet height is y and your distance traveled is x. The bullet starts at some distance above the ground, b. As it travels, x increases. The bullet then drops per unit x traveled by coefficient m.
i live in the UK, we don’t get tought that until college so i have a year until i learn that