These two scripts (examples/GunScripts/Bullet.gd
, examples/GunScripts/Gun.gd
) can be used to quickly bootstrap guns and physics simulated bullets.
Check out the examples/
directory for working scenes.
Attach the scripts to your Gun/Bullet scenes and examine the Script Variables
that appear.
auto_fire
- shoot automatically as soon ascan_fire
is set to true. Disable if gun will be shot manually.fire_delay
- time span between shots wherecan_fire
is falsereload_delay
- time it takes to reload a clip of ammunitionammo
- max ammo of the weapon, once it hits 0 the weapon cannot fire anymore. set to-1
for infiniteclip_size
- number of bullets in a clip/magazine. Bullets in the same clip have afire_delay
between them before the clip empties andreload_delay
must be calledshots
- an array of strings that reference bullets scenes. The whole array will be fired every volley
- setters -
set_clip_size
,set_can_fire
(will begin auto firing if it's true),set_ammo
reload()
- setscan_fire
to false, waits forreload_delay
before shooting againfire()
- exits ifcan_fire
is false. spawns a new bullet scene for every bullet inshots
. Sets vars up for the next bullet volley.
volley_fired(bullets_array)
- all the bullets inshots
have been created. To connect to bullet signals, callconnect
on each bullet in the passed array.out_of_ammo()
- ammo has hit 0 and gun cannot be shot until more ammo is addedclip_empty()
- reload() must be called before firing againcan_fire_again()
- gun is ready to shoot
Gun.gd is expecting to be placed upon a Node2D (or Node2D child). It requires a Node2D child named "GunSprite".
fire_pos_offset
- coordinate offset of bullet's spawn location upon the gun. Useful for placing bullet spawn location on a sprite, and ensuring bullets don't overlap when multiple are fired.follow_gun
- if true the bullet will be added as a child to the gun, and will move with the gun. Useful with laser beams.size_scaling_velocity
- added to the bullet's sprite's scale every frame, up to amax_size_scale
. Again, useful for laser beamsmax_size_scale
- seesize_scaling_velocity
fit_collider_to_sprite
- automatically resizes bullet's polygon to perfectly fit its sprite.deleted
- set to true when the object iskill()
and thusfree()
kill_on_collide
: killed on first contact, requires the body to enableContact Monitor
andContacts Reported
>0kill_viewport_exit
: killed when leaving view port, doesn't always work if spawned outside of view port and never enters the view portkill_after_time
: killed after a set amount of time, -1 disableskill_travel_dist
: killed after traveling a certain distance (may be removed as Time is similar and more performant), -1 disables
Notice there is no speed
.
Speed is found using the magnitude of your bullet's Linear Velocity (rigid body) property, and is set in the direction the gun was pointing when fired.
- setters -
set_fit_collider_to_sprite
,set_kill_after_time
,set_kill_on_collide
,set_kill_travel_dist
,set_kill_viewport_exit
setup(gun_fired_from)
- saves the fun, sets parent node, sets pos on gun, sets velocity, that sort of thing. Must be called when spawning from a gun (done automatically for consumers, shouldn't have to worry about this unless you are extending the classes).set_target(target_node, PID_Kp, PID_Ki, PID_Kd)
- sets the node the bullet should track, utilizing its PID controller (ref1, ref2) for targeting calculations.PID_K?
parameters are the gain (scalar) multiplied by different types of angle error (p = proportional/current, i = integral/sum of past d=derivative/speed of change). If negative, these values will not set the class variables (will be ignored).
bullet_killed(self)
- bullet has met some kill condition and has beenfree()
'd. Consider releasing some particles and such.
Bullet.gd is expecting to be placed upon a RigidBody.
It needs a CollisionShape2D child named "CollisionPolygon" and, if you are using fit_collider_to_sprite
, a "Sprite" child.
Bullets collide with character: Usually a gun will be placed on/inside a ship, character, etc's body. To ensure it does not collide with its overlapping body, you must modify Collision Layers and Collision Masks.
Gun is too boring: Connect particle effects and animations to Gun/bullet signals to juice things up.
Bullet physics are wonky/weird/bad: Try modifying the params on your bullet's rigidbody scene. Turn off gravity, friction, increase speed, etc.
Always welcome!
- Enable bullets for path follow in addition to physics
- Make applicable to 3D
MIT License (MIT)
Copyright (c) 2017 James R. Lowrey
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.