How do I stop my laser from destroying two items with one shot? (game design)

  • Thread starter Thread starter Darkmisc
  • Start date Start date
  • Tags Tags
    Collision Laser
Click For Summary
SUMMARY

The discussion centers on a game design issue where a player's laser unintentionally destroys multiple targets (enemies and enemy lasers) with a single shot. The developer's initial approach involved using the queue_free() method upon collision detection, but overlapping collision shapes led to unintended behavior. Suggestions included implementing a tag to track whether the laser has been used and reducing its strength after the first hit to prevent multiple destructions. The conversation also explored the potential for adding game mechanics like shields and reflections to enhance gameplay.

PREREQUISITES
  • Understanding of Godot Engine scripting, specifically GDScript
  • Familiarity with collision detection and handling in game design
  • Knowledge of game mechanics related to object tagging and state management
  • Experience with vector mathematics for collision detection optimization
NEXT STEPS
  • Implement a tagging system for laser shots to track usage status
  • Research methods for reducing projectile strength after initial impact
  • Explore the integration of shields and reflective surfaces in game mechanics
  • Study vector mathematics for efficient collision detection algorithms
USEFUL FOR

Game developers, particularly those working with the Godot Engine, and designers focused on collision mechanics and gameplay balance will benefit from this discussion.

Darkmisc
Messages
222
Reaction score
31
TL;DR
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]:

[CODE title="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()[/CODE]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
 
Last edited by a moderator:
Technology news on Phys.org
It's a feature!
 
  • Like
  • Haha
Likes   Reactions: Darkmisc and jedishrfu
Give the laser_shot object a tag that says if it has been used.
 
  • Like
  • Love
Likes   Reactions: Darkmisc and Tom.G
As dave said why not consider it a feature. Perhaps you could reduce the strength of the laser after it hits the first object so that its not as strong when it hits the next one.

Now you could add shields that deflect some or all of the laser light and allow for reflection hits and all sorts of unpredictable things.

I remember playing laser tag once (corporate team building exercise) and realized you could shoot at the mirrors in the room to get a player and I quickly racked up points until others caught on. I may have shared my secret in the interests of fair play then again maybe not.
 
  • Like
Likes   Reactions: Darkmisc
Baluncore said:
Give the laser_shot object a tag that says if it has been used.
Do you mean something like a global variable used = true/false?
 
jedishrfu said:
As dave said why not consider it a feature. Perhaps you could reduce the strength of the laser after it hits the first object so that its not as strong when it hits the next one.

Now you could add shields that deflect some or all of the laser light and allow for reflection hits and all sorts of unpredictable things.

I remember playing laser tag once (corporate team building exercise) and realized you could shoot at the mirrors in the room to get a player and I quickly racked up points until others caught on. I may have shared my secret in the interests of fair play then again maybe not.
Coincidentally, I have mirrors in my game too. I need the lasers fired to match the number of items destroyed because if they don't, the extra laser will reflect off a mirror and kill the player.
 
Doesn't that make the game more exciting?

Its like the scene in The Hunt for Red October where the Russian captain of the hunter sub shoots a torpedo but removes some distance limit allowing the torpedo to miss Red October and circle around and kill his own sub.
 
I think so. It stops the player from spamming lasers and forces them to be careful.
 
Darkmisc said:
Do you mean something like a global variable used = true/false?
It could be global, but I thought a laser would be an object with parameterised performance, activity status and variables such as location.x and .y, and the direction of fire. That way, you can have an array of lasers and an array of enemies operating at the same time. If the direction of fire was a unit vector, then collision detection between the enemy and the laser beam could be done very quickly by subtracting vectors as complex numbers and comparing ratios.
 
  • Like
Likes   Reactions: Darkmisc
  • #10
How come the laser doesn't get destroyed when it hits the first target? I mean, what prevents the laser from hitting a second enemy or laser, and why isn't it working when "they are close together"?
 
  • #11
jack action said:
How come the laser doesn't get destroyed when it hits the first target? I mean, what prevents the laser from hitting a second enemy or laser, and why isn't it working when "they are close together"?
I'm not sure. The laser should queue_free upon entering the collision shape of an enemy or enemy laser.
When I instanced the laser in front of the player, there was a chance that it would appear inside the overlapping collision shapes of the enemy and enemy laser. It made sense that the player laser might destroy both items before it queued free.

Now, I'm instancing the laser within the player. It should have to travel left to right before it hits something. In theory, it should only hit one item at a time, unless there is a precise moment when the enemy and enemy laser collision shapes perfectly overlap, and the player laser strikes at this moment. However, the player laser is destroying two items with one shot too often for me to think that this is the cause.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
10K
  • · Replies 2 ·
Replies
2
Views
10K