- Home /
Physics2D checking if object goes out of OverlapArea
I'm trying to do ladder in my2Dplatformer and i wanna check if playeris in range of ladder. And it works when he gets in but it doesn't detect when player goes out of range of ladder and causing enemy to fly.
So here's part of my code; this LayerMask is set to good layer.
[SerializeField] LayerMask playerLayer;
void Update() {
Collider2D coll = Physics2D.OverlapArea(pointA.transform.position, pointB.transform.position, playerLayer);
if (coll.gameObject != null)
{
// switch on climbing
}
if (coll.gameObject == null)
{
// switch off climbing, this if doesnt work dont matter what i put in
}
}
Answer by Zaeran · Mar 20, 2021 at 04:41 PM
OverlapArea only returns colliders that are inside the area. Once the collider is outside of the overlap area, OverlapArea no longer returns any collider. coll will be null, so coll.gameobject can't be accessed. Similarly, any collider that gets returned will always have a gameobject assigned to it.
Try
if(coll == null)
Your answer
Follow this Question
Related Questions
[2D] How to get gameobjects within a tilted rectangle area 0 Answers
physics Material 2D not working !!!! 1 Answer
How to use Physics2D.OverlapCircleNonAlloc for ground check and jump 0 Answers
Steering Separation Behavior: objects move endlessly from center 0 Answers
While Moving Left or Right my character falls more slowly. 2 Answers