#Check whether two 2D objects are overlapping
1 messages · Page 1 of 1 (latest)
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
- a raycast (or boxcast, etc) going downwards
- checking CollisionEnter/Exit for ground collisions (though this kinda depends on your level geometry)
- a collider/trigger around the feet specifically for option 2
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?
the hexagon can rotate?
a raycast basically shoots off a ray and sees if it hits anything
in that case you'd need even more consideration for the corners probably... though maybe that's not so bad, with something like this
though that'd probably require some playtesting to see if it feels right
well, it's not really a concern of practicality - if you want the player to rotate in gameplay, then work around that
its platforming, so prolly not
well, it being platforming doesn't really affect it
there's a pretty clear reason there
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?
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
Would I put the overlap checker in the script for the player or the fan, I guess the fan
in the fan for something like that, yeah
And when it detects the player, it should increment its speed in a certain direction, how would I get the rigidbody of the player?
you get it in the query/collision/trigger
it's accessible through the collider/collision you get
(in general consider if it's something the player does or if it's something the other thing does)
https://docs.unity3d.com/ScriptReference/Physics2D.OverlapBox.html isnt this exactly what that is?
that's one of the things i mentioned, yes
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
not sure what you're looking at
no wait the size is a Vector2 so the first param is the x lenght and the second is y lenght
no half anything is used there
no, as documented, the first param is a Vector2 point, for the center of the box, and the second param is a Vector2 size, for the size of the box - size.x represents the x size, and same for y
that's the x component, referring to it as that would probably be less ambiguous lol
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?
for something like this you'd probably do it in FixedUpdate
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
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
!code show code please
📃 Large Code Blocks
Use links to services like:
https://paste.mod.gg/, https://hastebin.skyra.pw/, https://paste.ofcode.org/, https://paste.myst.rs/
📃 Inline Code
Surround code with three backquotes. Not quotation marks.
To format as C#, add cs to the first line:
```cs
// Your code here
```
Add a comment with a line number if there is an error message.
i think i see the issue but it's really hard to tell with the small unformatted text
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
I think thats because it counts itself as overlapping with itself
there is no "itself" according to the setup you have there
oke
though, a few notes
a) you should use local positioning, so you can reuse this.
b) why not just use a collider here
whats a local position?
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
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
sure, but any other collider2d would work similarly if you wanted to represent a different shape
a rectangle works for me
(set to trigger in this case, since you don't want it to be solid)
is that a settting in the editor ror not? I cant seem to find
it's a setting on the collider
i am so incredibly smart i made a whole loop and am now on the same level as a monkey istg
that's just inexperience, it'll get better with time
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?
right
a collider2d set to trigger gives 3 messages, OnTrigger[Enter/Stay/Exit]2D
you can use them to react to stuff happening
if you are using a character controller, it should handle this for you, but i could be wrong. If you are using a rigidbody, the way I usually solve this is to have a counter that ticks up on a collision entered and ticks down on a collision left.
CharacterController is for 3d
checking for ground collisions can work, but it depends on the level geometry as to whether that's reliable
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
not that i know of
oh really? thats honestly crazy to me
such a basic feature missing
i was thinking of 2d capsule colliders
i mean, it's not a unity feature to begin with
it should be
yeah it only works for checking if its touching anything of a specific layer, if you want to use it for something more complex it wont be a good match
charactercontroller comes from physx, the default 3d physics engine
the default 2d physics engine is box2d
that's why they have subtle differences lol
oh I see
like 2d having static rigidbodies, there being different restrictions on kinematic rigidbodies, that kind of thing
but i kind of assumed that this wasn’t the player, idk have no idea the surrounding context
how gravity is configured differently
the message starts off with "My player is a hexagon" lol
eh, you just missed a few words, nothing weird. happens all the time lol
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
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
so ur forced to wrangle a complex simulation in order to o produce simple behavior
say what you will about godot and g amemake but they support more simple 2D character physics put of the box
raycasts and dedicated colliders are still pretty simple anyways
no reason for unity to be behind here imo but im going off topic
2d character controller
that's such a simple thing to make, does it even need to be its own thing
its not simple unfortunately, as this thread shows
it would be so nice to be able to just controller.move()
transform.Translate exists
rigidbody.MovePosition exists
rigidbody.velocity exists
move and collide
cool, rigidbody velocity.
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)
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
you wouldn't use a charactercontroller for that anyways
isn't that problem in particular really easy to solve anyways
so you basically just want what CharacterController gives, but in 2d?
yes
unfortunately it is not
not crazy hard but
seems like box2d is looking into it
https://box2d.org/posts/2025/04/box2d-3.1/
Version 3.0 was released about 8 months ago. It was a huge update and it needed some refinement. Version 3.1 addresses most of the bugs and build problems. It also introduces some new features and refinements.
Character mover I’ve added experimental character mover support to 3.1. This uses a geometric solver. It should be useful for games tha...
nice
just cache the current velocity yourself, no?