0 votes

I think it might have been possible in 2.X with the trigger property, but that's gone. It's still possible by having a rigidbody with a disabled collision shape and adding the area 2d as a child, but that seems wasteful when I've got thousands of bullets on screen at once. Am I missing something obvious?

asked Mar 11, 2018 in Engine by markopolo (618 points)

1 Answer

0 votes

Do your bullets really need physics? When I've done bullets, I've made them Area2D's and used the Area2D's signal on_body_entered to detect the hits.

answered Mar 16, 2018 by zdimaria (201 points)

There's not an easier way to have movement "just work" - I'd have to write the movement code, and that seems wasteful when there's a perfectly good physics engine avaiable :)

Its actually super easy, and uses way less resources to move them (you mentioned you'll have thousands on screen).

If you're interested, this is how you could set it up:

Make a Bullet.tscn -- an Area2D with a Sprite2D and CollisionShape2D as it's children.

Add a script to Bullet.tscn:

var SPEED = 400

func _ready():
    connect('body_entered', self, '_on_body_entered')

func _process(delta):
   var motion = Vector2(cos(self.rotation), sin(self.rotation)) * SPEED
   position += motion * delta

func _on_body_entered(body):
   print('bullet just hit: ', body.name)

And then in the scene that you want to shoot the bullet, make an instance of the bullet and set its rotation to the direction you want it to go in.

Thats it! if you don't understand anything about the script i'd be more than happy to walk you through it.

"uses way less resources to move them"

I'm not sure I believe you that user scripts are more efficient than the physics engine :) I definitely would be interested to know how that shakes out performance-wise, especially when multithreaded physics are enabled.

One use case that the engine handles nicely is bullet deflection - let's say I want to have bullets "bend" their paths when in some area (think Auditorium but with bullets). It's pretty easy to say body.apply_impulse when it's in the area - I can even customize the effect by changing the bullet mass to have some bullets be more resistant than others. I'm not really inclined to figure out how to do that outside the physics engine :\

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.