Hi.
I’m doing a platformer game where the protagonist has a sword. Sometimes there are objects he has to smash several times for being able to advance.
For the sword I have a simple BoxCollider2D set with IsTrigger and I enable it whenever i need to attack (Just an Input.getkeydown) and it keeps active for 0.25 seconds before disabling it again. If I keep pressing the attack button I repeat the attack in the same pattern, I don’t move the collider in all this time, only enabling and disabling it. This collider has a Kinematic RigidBody2D attached set with Continuous Collision Detection.
The Object I need to smash has another BoxCollider2D and RigidBody2D with the same configuration as the sword and a “Destructible” layer asigned.
For the Sword Script I have just a simple code:
void OnTriggerEnter2D(Collider2D col) { if (col.gameObject.layer == LayerMask.NameToLayer("Destructible")) Debug.Log("Hitting Object"); }
When I make the collider appear, the OnTriggerEnter reacts and shows the “Hitting Object” message. The problem is that the following hits, as neither Collider2D moved (only enabled and disabled one of them), Unity don’t recognize the “OnEnterTrigger” anymore.
I would like to know if there is any way around it because its making me frustrated, I dont want to make a random move to the collider so it detects the OnTriggerEnter again.
I thought about using Physics2D BoxCastAll (as it can hit several objects) but as I need to be able to hit for 0.25 secs and only once I dont want to use a Dictionary of hit objects, or something similar, for the BoxCast duration.
I really appreciate any help you can provide