Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Paar |
Hi. I have a little experimental project where I test some functionalities. I have worked out everything I have needed so far apart from a flying bullet which is represented by Area2D node.
The problem I’m trying to solve is that when the bullet hits a PhysicalBody and when hide() is called I can see the bullet several frames after the hit (so it travels behind the border of the object’s collision shape). Is there a way to hide the bullet right at the moment when body entered it with just Area2D?
Please excuse the code as it’s a little messy (it’s experimental as I wrote before). Thank you.
extends Area2D
var bullet_direction
var shape_owners = []
var overlapping_bodies = []
const BULLET_SPEED = 400
func init(direction):
bullet_direction = direction
func _ready():
shape_owners = get_shape_owners()
overlapping_bodies = get_overlapping_bodies()
if overlapping_bodies.size() > 0:
hide()
func _process(delta):
if detect_overlap_world():
hide()
for x in shape_owners:
shape_owner_clear_shapes(shape_owners[x])
queue_free()
translate(bullet_direction * BULLET_SPEED * delta)
overlapping_bodies = get_overlapping_bodies()
var enemyBody = get_overlap_enemy()
if enemyBody:
hit_enemy(enemyBody)
func detect_overlap_world():
if overlapping_bodies.size() > 0:
for x in overlapping_bodies.size():
if !overlapping_bodies[x].is_in_group("Enemy"):
return true
return false
func get_overlap_enemy():
var overlapping_bodies = get_overlapping_bodies()
if overlapping_bodies.size() == 1:
if overlapping_bodies[0].is_in_group("Enemy"):
return overlapping_bodies[0]
return
func hit_enemy(body):
body.get_hit()
queue_free()