#Check whether two 2D objects are overlapping

1 messages · Page 1 of 1 (latest)

raven heron
#

My player is a hexagon, and I need to check whether the hexagon is on the ground. I have a Ground layer for the things that I consider ground, and also a slightly bigger hexagon around the player that can overlap with objects of the Ground layer, how do I do this?

granite apex
#

there are plenty of ways to detect grounded state, like using a raycast or detecting collisions. if that "slightly bigger hexagon" is centered on the player, you probably wouldn't want to use that to check grounded state, it'd also say you're grounded if you go up against a wall

#

i'd suggest a different approach, perhaps one of these

  1. a raycast (or boxcast, etc) going downwards
  2. checking CollisionEnter/Exit for ground collisions (though this kinda depends on your level geometry)
  3. a collider/trigger around the feet specifically for option 2
raven heron
#

I tried option 3, but then I would have to add 6 colliders for all 6 sides of the hexagon

#

How does option 1 work? How do I do that?

granite apex
granite apex
#

though that'd probably require some playtesting to see if it feels right

raven heron
#

wait maybe I should disable that

granite apex
#

well, it's not really a concern of practicality - if you want the player to rotate in gameplay, then work around that

raven heron
#

its platforming, so prolly not

granite apex
#

well, it being platforming doesn't really affect it

raven heron
#

Yeah that fixezs it!

granite apex
#

there's a pretty clear reason there

raven heron
#

Yeah I know but ive never seen that before

#

But what about in general? Say I want to make an object that is a giant fan that can push the player, how would I make it detect whether the player is in the stream of air?

granite apex
#

you would do a trigger volume or a boxoverlap/boxcast, probably

#

there's only really 3 ways to detect stuff through physics

  • physics queries (casts/overlap checks)
  • collision messages
  • trigger messages
raven heron
#

Would I put the overlap checker in the script for the player or the fan, I guess the fan

granite apex
#

in the fan for something like that, yeah

raven heron
#

And when it detects the player, it should increment its speed in a certain direction, how would I get the rigidbody of the player?

granite apex
#

you get it in the query/collision/trigger

#

it's accessible through the collider/collision you get

granite apex
raven heron
granite apex
#

that's one of the things i mentioned, yes

raven heron
#

wait isn't a box with the definition given in that doc a square?

#

it has a center and a half lenght, but not a radius or a half length x or half lenght y

granite apex
#

not sure what you're looking at

raven heron
#

no wait the size is a Vector2 so the first param is the x lenght and the second is y lenght

granite apex
#

no half anything is used there

raven heron
#

then im confusing words

#

nvm then

granite apex
raven heron
#

yeah yeah

#

i meant the first param of the constructor of Vector2

#

of the size

granite apex
#

that's the x component, referring to it as that would probably be less ambiguous lol

raven heron
#

ye

#

So I every frame I would call Physics2D.OverlapBox() and if there is something inside that it returns that object, and if it is a rigidbody2d I increment its speed by some amount right?

#

but it returns a collider, how do i get the rigidbody attached to it, if there is one at all?

granite apex
granite apex
#

also consider using OverlapBoxAll in this case - the fan should (logically) affect anything in its way
this would make it easy to add, for example, other players or movable objects

raven heron
#

I tried this, but it doesn't seem to work. I placed a box with the exact size and coordinates as the hitbox shown, but it doesn't log

waxen loomBOT
granite apex
#

i think i see the issue but it's really hard to tell with the small unformatted text

raven heron
#

oh

#

well I changed the code a bit it now looks like this

using UnityEngine;

public class FanScript : MonoBehaviour
{
    void Start()
    {
        Debug.Log("Fan started");
    }

    void FixedUpdate()
    {
        Collider2D[] things = Physics2D.OverlapBoxAll(
            new Vector2(1.5f, 0.5f),
            new Vector2(1, 3),
            0
        );

        Debug.Log(things.Length);

        /*
        foreach (Collider2D thing in things)
        {
            Debug.Log(thing.name);
        }
        */
    }
}
``` it keeps logging 1, I think thats because it counts itself as overlapping with itself
granite apex
#

I think thats because it counts itself as overlapping with itself
there is no "itself" according to the setup you have there

raven heron
#

oke

granite apex
#

though, a few notes
a) you should use local positioning, so you can reuse this.
b) why not just use a collider here

raven heron
#

whats a local position?

granite apex
#

right now you have these hard-coded values

new Vector2(1.5f, 0.5f),
new Vector2(1, 3),
if you wanted to make a fan somewhere else, you'd have to make an entirely new script, or if you wanted to move this one, you'd have to change the code along with changing the position in the scene - that can easily lead to mistakes that cause weird, hard-to-track bugs

this uses "global" or "absolute" positions and sizes- these values are relative to the world, unrelated to anything on the current object
if you make it "local" or "relative", you can move this object anywhere or create new fans anywhere without modifying the values in code

#

(you'd also want to make the offset/size serialized so they're configurable in the editor as well, in that case)

#

but option b) just kinda sidesteps all of this, you get to modify it all in the editor

raven heron
#

aha

#

thats fair

#

what about a collider

granite apex
#

why not just use a collider to represent that space, instead of having the values in code

#

that way you can easily visually configure it in the editor

raven heron
#

that sounds way easier

#

you mean a box collider 2d right

granite apex
#

sure, but any other collider2d would work similarly if you wanted to represent a different shape

raven heron
#

a rectangle works for me

granite apex
#

(set to trigger in this case, since you don't want it to be solid)

raven heron
#

is that a settting in the editor ror not? I cant seem to find

granite apex
#

it's a setting on the collider

raven heron
#

yeah yeh

#

everything is grayed out, and i still cant find it

granite apex
#

the collider is collapsed there

#

you're looking at the material

raven heron
#

i am so incredibly smart i made a whole loop and am now on the same level as a monkey istg

granite apex
#

that's just inexperience, it'll get better with time

raven heron
#

okay i made it a trigger, i assume I can now write a general script for a fan (with the local position ofc) that uses the trigger?

granite apex
#

right

#

a collider2d set to trigger gives 3 messages, OnTrigger[Enter/Stay/Exit]2D

#

you can use them to react to stuff happening

raven heron
#

Stay looks like the option i desire

#

Yippie :D

weak pond
granite apex
weak pond
#

theres a 2d character controller now no?

#

also how so

granite apex
# weak pond also how so

you wouldn't want to use the same logic for colliding with the ground, a wall, or a ceiling
if you have floating platforms, you might have it be a single collider on the Ground layer for simplicity, and you'd end up being able to jump off the side or the bottom
even if you want to allow wall jumps, you'd want to have separate logic

granite apex
weak pond
#

oh really? thats honestly crazy to me

#

such a basic feature missing

#

i was thinking of 2d capsule colliders

granite apex
weak pond
#

it should be

weak pond
granite apex
#

charactercontroller comes from physx, the default 3d physics engine
the default 2d physics engine is box2d

that's why they have subtle differences lol

weak pond
#

oh I see

granite apex
#

like 2d having static rigidbodies, there being different restrictions on kinematic rigidbodies, that kind of thing

weak pond
granite apex
#

how gravity is configured differently

granite apex
weak pond
#

oh im dumb

#

i am curious why it would he a hexagon

granite apex
#

eh, you just missed a few words, nothing weird. happens all the time lol

weak pond
#

unless its a physics based thingy

#

a capsule or box is much better suited to gameplay (box easier to solve for)

#

if u avoid slopes u can use the collision tracking and track ground and walls seperately

#

even with straight slopes it could work

#

depending on geometry

#

i remember doing that for a platformer once

#

but it will always annoy me how cumbersome it is to handle basic collisions like this

#

most 2D games dont need a realistic physics simm

granite apex
#

oh actually you could use collision messages regardless of level geometry if you check for where the collision happened, i forgot about that

#

that seems a bit.. overly complex for a simple thing though

#

i haven't really considered that solution before

weak pond
#

so ur forced to wrangle a complex simulation in order to o produce simple behavior

granite apex
#

to make it generalizable, yeah

#

(i mean, complex here is relative)

weak pond
#

say what you will about godot and g amemake but they support more simple 2D character physics put of the box

granite apex
#

raycasts and dedicated colliders are still pretty simple anyways

weak pond
#

no reason for unity to be behind here imo but im going off topic

granite apex
#

..it isn't?

#

what do you think unity is missing

weak pond
#

2d character controller

granite apex
#

that's such a simple thing to make, does it even need to be its own thing

weak pond
#

its not simple unfortunately, as this thread shows

#

it would be so nice to be able to just controller.move()

granite apex
#

transform.Translate exists

#

rigidbody.MovePosition exists

#

rigidbody.velocity exists

weak pond
#

move and collide

granite apex
#

cool, rigidbody velocity.

granite apex
# weak pond its not simple unfortunately, as this thread shows

using collision messages to detect grounded state is the thing we're saying is complex here, no?
making a character controller in general isn't super complex - especially when you know your own constraints
of course it gets more complex with more features, and a built-in controller has to have a lot of features to be very generalizable

#

(also like i mentioned before, this isn't even unity's system)

weak pond
#

doesn’t move until physics update, doesn’t give simple immediate collision info, and has built in behavior that might be unwanted

#

if you want a projectile to bounce off a wall you cant just reflect the velocity bc thr velocity has changed by the time u get the message

#

so u lose control

granite apex
#

you wouldn't use a charactercontroller for that anyways

#

isn't that problem in particular really easy to solve anyways

granite apex
weak pond
#

yes

weak pond
#

not crazy hard but

granite apex
# weak pond yes

seems like box2d is looking into it
https://box2d.org/posts/2025/04/box2d-3.1/

weak pond
#

nice

granite apex
weak pond
#

that comes with its own issues, speaking from expirence

#

not justt because its dirty code

#

its fine

#

just has always bugged me how not simple so many basic game things are