I have a game, where i the players has 4 collider… (Top bottom left and right)
And whenever something hit the player, i have to detect which collider caused the event… But i onlycan get the GameObject (which is either the ground or the player) but cannot dig out the Collider2D
Morning, just a couple of thoughts from me ![]()
is it a compounded collider for a strange player shape? if it is I dont think its going to be easy to distinguish between them unless you have them on different child gameobjects of the player.
however, you could use a compound collider or even one collider (if it fits the player shape) and check if the collision ocurred to the left/right above/below by comparing the players transform x,y against the colliding objects x,y within the OnCollisionEnter2D() or OnTriggerEnter2D() depending what your using.
ive been working through a tutorial series, if its definitely multiple colliders your after, have a look at it, its for a match-3 game, however the concept might be interesting.
The player gonna be square shaped… But there would be more collision at the same time possible… So maybe it collided to the wall and the ground at the same time…
Buecause i need to Destroy the ground i have to decide whether which GameObject collided tot he bottom collider…
ah ok, yeah if your going for the route of multiple colliders and wanting to differentiate, i would go for putting it in a child gameobject of the player, it does that in the above vid to give an idea on how it works. ive not really tried it to much as i went via the raycasting method eventually ![]()
I had a similar problem as I had sort of sinking platforms that eventually disappear and I had one-way platforms, but i gave the wall game objects a tag, so in my collision checks, i could check if it was the wall, and if it was do nothing, or if it was the floor or roof do something else.
this is kind of what i used initially before changing to raycasting approach.
void OnTriggerEnter2D(Collider2D col)
{
if (col.tag == "Wall")
{
// check where the collision is relative to my position.
// do something pertaining to collision with wall
}
if (col.tag == "Ground")
{
// check if it is above or below me
// do something pertaining to collision with ground after checking its position.
}
}
if you dont mind me asking, is it a 2D platformer your doing? using several block sprites with colliders to make a large platform?
That won’t work
Because the world is build from different squares…
Something similar the Minecraft (not the game)So there is a square at side of the player there is under and there can be above too…
They are generated with a script, from an array so i can’t tag them differently…