Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | ThEnderYoshi |
To boil it down, I’m trying to make a bullet collide with a player every frame.
However, the situation is pretty complex, so I’ll try to explain the structure here:
I have a general Battle
scene:
There are multiple Encounter
scenes.
One is chosen at the start of the battle, then added to CurrentEncounter
:
The Encounter
node has an Array[PackedScene]
.
Each PackedScene
contains a Wave
scene:
Here is a Wave
. One is randomly chosen, then added to CurrentWave
.
The Bullet
node is the Area2D
in question:
When the Wave
loads, it instances the Player
scene, then adds it as a child:
# Part of wave.gd
func _ready() -> void:
# This is for debugging
if get_tree().current_scene == self:
setup()
start()
func setup() -> void:
# Called IMMEDIATELY after instancing
rect_min_size = rect_size
_create_bounds()
func start() -> void:
# Called when the player should spawn
player = SCENE_SOUL.instance()
add_child(player)
player.owner = self
player.position = get_node("PlayerSpawn").position
To collide with the player (KinematicBody2D
), the Bullet
has this:
# Part of bullet.gd
extends Area2D
# ...
onready var wave: Wave = owner
# ...
func _physics_process(delta: float) -> void:
if wave.soul in get_overlapping_bodies():
wave.soul.damage(at)
And here’s how the Wave
is loaded in Battle
:
# Part of one of Battle's children
func enter() -> void:
var next_wave = battle.encounter.get_wave()
next_wave.setup()
battle.current_wave.add_child(next_wave)
battle.anim.play("show arena")
yield(battle.anim, "animation_finished")
next_wave.start()
With that out of the way, here’s the problem:
When I run the Wave
scene by itself, everything works as expected.
But when I test it in the Battle
scene, the Bullet
just doesn’t collide with the Player
.
(The Bullet
’s and Player
’s collision layers and masks are compatible.)
Below is a list of things I’ve tried, while getting the same results:
-
Replacing
wave.soul in get_overlapping_bodies()
withoverlaps_body(wave.soul)
-
Using signals
For the record, in the case where it didn’t work, the Player
can still collide with physics bodies, just not the Area2D
ThEnderYoshi | 2022-09-04 04:20