I am new to Godot and following a tutorial, which spawns enemies randomly. Nonetheless, I am attempting to enhance on this by checking that the enemies do not spawn on prime of the participant and trigger an on the spot loss.
At first I attempted utilizing _on_area_entered on the Enemy, checking if the realm entered was the Participant (with is_in_group), and selecting a brand new place in that case. Nonetheless, this does not appear to work as a result of the _on_area_entered in Participant additionally detects the collision and the participant dies. It looks like collisions are detected earlier than I replace place, and never after.
So then I attempted spawning CollisionRectangle2D when spawning the Enemy, however this did not work and I am nonetheless not solely certain why. The essential concept was to spawn colliders and put them within the tree and test in the event that they collide with the participant, after which set the enemy to their place if not. Nonetheless, I have to be doing one thing fallacious with the coordinates, as a result of this by no means logs “bonk”:
func set_spawn() -> void:
var collision_tester = CollisionShape2D.new()
collision_tester.form = $CollisionShape2D.form
var player_shape: CollisionShape2D = get_tree().get_first_node_in_group("participant").get_collision_shape()
var loop_stop = 100
collision_tester.remodel.origin = Vector2(randi_range(0, screensize.x), randi_range(0, screensize.y))
whereas collision_tester.form.collide(
collision_tester.remodel,
player_shape,
player_shape.remodel
) and loop_stop > 0:
print("bonk")
loop_stop -= 1
collision_tester.remodel.origin = Vector2(randi_range(0, screensize.x), randi_range(0, screensize.y))
place = collision_tester.remodel.origin
I assumed possibly there was some type of deferral the place the physics state and the positions desynch in a single body? So I attempted utilizing PhysicsShapeQueryParameters2D:
func set_spawn() -> void:
var question = PhysicsShapeQueryParameters2D.new()
question.collide_with_areas = true
question.form = RectangleShape2D.new()
question.form.measurement = $CollisionShape2D.form.measurement
question.remodel.origin = Vector2(randi_range(0, screensize.x), randi_range(0, screensize.y))
whereas get_world_2d().direct_space_state.intersect_shape(question, 1).measurement() > 0:
print("Bonk")
question = PhysicsShapeQueryParameters2D.new()
question.collide_with_areas = true
question.collide_with_bodies = true
question.form = RectangleShape2D.new()
question.form.measurement = $CollisionShape2D.form.measurement
question.remodel.origin = Vector2(randi_range(0, screensize.x), randi_range(0, screensize.y))
place = question.remodel.origin
$CollisionShape2D.set_deferred("disabled", false)
Nonetheless, that does not work both – it logs “bonk” after which simply … the participant nonetheless dies.
My kind-of-working venture code is on-line at https://github.com/ertyseidohl/coin-dash, at 60d1196 on the time of writing.