godotgodot4

How do I temporarily disable physics collision on a RigidBody in Godot?


I am both new to Stack Overflow and Game Development, so sorry if this question is stupid. I am currently working on a game in godot where a player can shoot projectiles at enemies (oversimplification). However, I have the following problem: The projectile spawns inside the player model and then collides with the player shooting it and bounces off. My projectile uses the RigidBody2D type and the Player is a CharacterBody2D.

I have tried the following:

Set the collision layer mask to be on a different layer than the player

Problem: The projectile is supposed to bounce off walls and it should then be able to hit the player. So, the two have to have collision.

Do the first suggestion but enable the mask layer after the first wall bounce

Problem: Later on, I want to use something similar to guided missiles. The player can shoot missiles that follow the mouse and then hit enemies or the player. So, the projectile should be able to collide with the player without a wall bounce.

Do the first suggestion but enable the mask layer after x amount of milliseconds

Problem: The projectiles have different speeds and the player can have models of differing size, so I would like to avoid something like this.

Spawn the projectile with an offset so that it starts outside the player hitbox

Problem: I tried this one and it looked off, especially with varying player models. But if there is no other solution, this would probably be the way to go.

Basically, I would like to disable collision between the two, then have the body_exited signal fire when the projectile leaves the player hitbox and then reenable collision. The problem is that if I disable the collision, the body_exited signal won't fire because there is no collision. But with collision enabled, the projectile bounces off the player. Basically, what I want is for there to be no physics interaction between the two initially but with active collision detection. Any help would be appreciated. Also, because I am new, best practice advice is very much appreciated. If what I am doing is considered bad practice, please let me know.


Solution

  • Honestly, I would use the offset, but have a different effect for the shooting animation.

    However, if you really want to do this, my suggestion is to use an area on top of the character to detect the projectile. As soon as the projectile leaves the area, you enable the collision for the projectile.

    This way, if the model/size/scale whatever of the character is different, you can aedjust the area accordingly. So you do not need to rely on an error prone timer.

    About disabling the collision, aside form disabling the collision shapes and changing the layers, you can add an exception from code using add_collision_exception_with, which you can then remove with remove_collision_exception_with. That way you can disable the collision between the character and the projectile without disabling anything else.