How can I make an CollisionObject2D ignore collision with another object, until it isn’t inside of it anymore?
Extra details
This project is 2D and top-down. I have a simple box (a CharacterBody2D with a square CollisionShape2D) which can be picked up by the player and by enemies. After being picked up, it can be thrown. The box resides on top of its holder. Illustrative picture:
To prevent the box from colliding when held, the box’s process mode is set to disabled: held_obj.process_mode = Node.PROCESS_MODE_DISABLED. It is then re-enabled when thrown.
I have tried to change the masks/layers of the thrower to something else for a brief moment. However, if something would throw a thing, while a thrown object is about to collide with them, it would also ignore the incoming collision.
So first off, you are crating a lot of future problems for yourself by making the box a CharacterBody2D. I’d recommend making it a RigidBody2D. You’ll have to change the throwing code, but you’ll have a lot less code overall. You can also update the physics of the box using a PhysicsMaterial.
Secondly, if you don’t want the box to collide with the player, better to change how it collides instead of how the thrower collides. The easiest way to do this is set the CollisionShape2D to disabled. Based on how you’ve described the box, this is all the code you should need using a RigidBody2D.
Then you just call pick_up() when you pick it up, and throw() when you throw it, and pass in the direction you want the box thrown. By modifying the weight of the box you can change the arc it flies.
I appreciate the reply, but it does not handle the actual question: “How to add a collision exception?”
The point of this topic not CharacterBody2D V.S. RigidBody2D, nor is it about how to disable a CollisionBody2D. Also, it is a top-down game, not a side-view game. I apologize if I sound passive-aggressive. I could have been more clearer in my original post. I left a lot of information out of it.
The problem is that the box rests on the thrower, if something is obstructing the box from being held directly in front of it. When the box is occupying the same space as the thrower, and is thrown, it immediately collides with it, causing both of them to be mispositioned.
Illustrative video:
I solved a part of the problem by not re-enabling the CollisionShape2D immediately, when thrown from “above”. However, I do not know a way of re-enabling it, after it isn’t “colliding” with the thrower anymore.
Illustrative video:
How can I ignore the collision between a thrown object and its thrower?