- #1
Darkmisc
- 203
- 27
- TL;DR Summary
- When the collision shapes for two on screen items overlap, the player's laser can sometimes destroy both items with one shot. The laser is only supposed to destroy one item per shot.
Hi everyone
I'm making a game in which a player's laser can destroy enemies and cancel out enemy lasers. My code for the player laser executes queue_free() when it enters the collisionshape for the enemy laser and enemy. The player laser is only supposed to destroy one item per shot.
Sometimes, the collision shapes for the enemies and enemy lasers overlap while they are very close to the player. If the player fires the laser at this moment, the laser will destroy both items.
At first, I thought the problem was that the player laser instances inside the collision shapes of the enemy and the enemy laser. This would eliminate both collision shapes with one shot.
I tried fixing this with the following code [from enemy_laser scene]:
It's supposed to detect when the enemy and enemy laser overlap. When this happens share_space == true.
If the laser enters while share_space==true, the laser is supposed to queue_free the element that is closest to the left of screen (I have corresponding code for this in the enemy scene).
This code didn't fix the problem.
Then I thought if I instanced the player laser further to the left (from within the player), it won't instance into the overlapping collision shapes. Instead, it should only enter a collision shape as the laser moves from left to right. However, the laser will still sometimes destroy two items with one shot (when fired close to the items).
Does anyone know how to fix this problem?
Thanks
I'm making a game in which a player's laser can destroy enemies and cancel out enemy lasers. My code for the player laser executes queue_free() when it enters the collisionshape for the enemy laser and enemy. The player laser is only supposed to destroy one item per shot.
Sometimes, the collision shapes for the enemies and enemy lasers overlap while they are very close to the player. If the player fires the laser at this moment, the laser will destroy both items.
At first, I thought the problem was that the player laser instances inside the collision shapes of the enemy and the enemy laser. This would eliminate both collision shapes with one shot.
I tried fixing this with the following code [from enemy_laser scene]:
enemy_laser:
func _on_enemy_laser_area_entered(area):
if area.is_in_group("enemy"):
ENEMYPOS = Global.enemies_pos
share_space = true
if area.is_in_group("laser"):
if share_space:
if position.x<= Global.enemy_laser_pos:
print("laserdestroyedpos=" + str(position))
Global.enemy_laser_pos=1280
$Sprite.visible=false
$CollisionShape2D.set_deferred("disabled", true)
if share_space == false:
Global.enemy_laser_pos=1280
queue_free()
If the laser enters while share_space==true, the laser is supposed to queue_free the element that is closest to the left of screen (I have corresponding code for this in the enemy scene).
This code didn't fix the problem.
Then I thought if I instanced the player laser further to the left (from within the player), it won't instance into the overlapping collision shapes. Instead, it should only enter a collision shape as the laser moves from left to right. However, the laser will still sometimes destroy two items with one shot (when fired close to the items).
Does anyone know how to fix this problem?
Thanks
Last edited by a moderator: