You want a 2D bullet that travels in an arc, or ballistic curve.
One approach to this problem would be to use a RigidBody2D
- with its built-in physics, gravity would automatically pull it back to earth after firing it.
However, as mentioned in the 2D shooting recipe, Area2D
is a great choice for simple bullets and other projectiles - when you don’t need collisions, bouncing, or other physics reactions. Ballistic motion is easy enough to calculate that we won’t need the help of the physics engine.
- Bullet (Area2D)
- Sprite
- CollisionShape2D
We can use Area2D
's gravity
property. Set it to 150
for the initial test.
extends Area2D
var velocity = Vector2(350, 0)
func _process(delta):
velocity.y += gravity * delta
position += velocity * delta
rotation = velocity.angle()
func _on_BallisticBullet_body_entered(body):
queue_free()
Using the standard equations of motion is all we need to do here. The initial value for velocity
is just for testing. Run the bullet scene:
Now in your object that’s doing the shooting, you can instance the bullet and set its initial properties. Put this in whatever function/input handles shooting:
export var muzzle_velocity = 350
export var gravity = 250
func shoot():
var b = Bullet.instance()
owner.add_child(b)
b.transform = $Barrel/Position2D.global_transform
b.velocity = b.transform.x * muzzle_velocity
b.gravity = gravity
Here’s an example in action: