- Home /
Detecting empty collider
Alright so I think I'm struggling with something syntax related, not entirely sure. Right now, this code just makes it so the tagged objects take damage - it works. Although, I wanna get another bit of code that detects if the hitbox hasn't collided with anything, so I can do some functions for when that scenario happens. Is there a simple of way of doing this that I'm missing?
You question is not clear enough, please provide a full description of what are you trying to achieve.
Ok so as of right now, when I press the down key, the player attacks and temporarily creates a hitbox, this code decides what happens when it collides with an "Enemy" object as well as a "bomb" object. When this hitbox is created, I want to be able to write some code for when the collider doesn't hit any other colliders. To be more specific, I want the field of view to shrink whenever the player misses (But I know how to do the shrinking part).
You need logic for then a trigger is not triggered. That will never work in OnTriggerEnter. Ins$$anonymous$$d, set a bool for when it got triggered, after the attack, check that bool if something was triggered, if not, do your logic and reset the bool.
Answer by GrayLightGames · Oct 12, 2019 at 06:25 PM
Collider2D has a member function called OverlapCollider that will give you a list of any colliders that currently overlap it. So if you call this from Update or when you need you'll be able to check it for null. That will only help if your other objects have colliders. Alternatively, you could always just make a derived class from Collider2D with a bool hasCollided that you would default to false and set to true on collision. That would give you flexibility in case you need to check the object after the triggering object/collider has already left. Hope that helps!
Yeah I was playing around with the second option for quite a bit - using booleans. Either I'm being dumb or they're sort of awkward to use in this scenario (I'm gonna assume the first one). Although, I've come up with a bootlegged solution where I've just added another collider which checks when the player attacks, running code when the player collides with that rather than anything else. Pretty grim right.
Not sure I follow, but sounds a bit bootlegged like you said... but hey if it works for you, why not? If you did just go with bools, it would just be something like:
bool hasCollided;
OnTriggerEnter2d(Collider2d col)
{
hasCollided = true;
//The rest of your code here
}
CheckHasCollided()
{
if(!hasCollided)
{
//Whatever your zero collision code is
}
//If you want to reset the collider after the check
hasCollided = false;
}
This wouldn't be too awkward unless I'm missing something, whatever event triggers the collider check can just call CheckHasCollided(). If that's a player attack, is there a function you're calling to make that happen? Even if you need separate bools based on the colliding tag, might be easier to deal with than the collider you're talking about. Like I said though, if you have something that works for you, run with it and let's hope the Best Practices police aren't reading this post :)
Alright so I tried exactly this before going to the whole bootlegged thing. When doing it like that (with the bools), it detects all the misses until I hit an enemy for the first time, which is detected as a miss for some reason. After that, it goes into a pattern that I can't describe, detecting enemies as a miss, misses as enemies. This then swaps round whenever I throw a miss into a spree of enemy hits or vice versa.
I put the CheckHasCollided function into the player script with all this stuff.
Your answer
Follow this Question
Related Questions
Tilemap Collider 2D preventing objects from moving 2 Answers
How do I get collisions between Tilemap Collider 2d and a Kinematic Rigidbody 2d? 1 Answer
Weird ContactPoint2D on two BoxCollider2D collision 0 Answers
Collision detection problem 0 Answers
How to make a PlatformEffector2D doesn't affect a specific layer? 1 Answer