- Home /
How to check if 2 collisions are on the same objects ? (is player on the same ground than enemy?)
Hi guys, I'd like to check if my groundCheck for player and groundCheck for enemy are colliding with the same ground.
My detection system is an OverlapCircle checking for a Ground layer.
So I want to look at my "collision" events for both detections and compare the objects concerned.
NB : all that is 2D Thanks !
Answer by GiyomuGames · May 25, 2016 at 07:55 AM
Simply compare the objects found in the "ground checks".
For example if you do the ground check using raycast you probably do RaycastHit2D hit = Physics2D.Raycast(transform.position, -Vector2.up);
or something like that. If you compare the hit.transform of your 2 resulting RaycastHit2D then you'll know if they are the same object or not.
I'm not really getting a "hit" from my cast, actually to see if player is grounded I'm just having a bool looking like this : standing = Physics2D.OverlapCircle (checkPosition, collisionRadius, standingLayer);
Of course I can change that, but even with your suggestion, how do I actually check if 2 transforms are the same ?! I know it sounds stupid but is it only something like (no code, only logic here) : - "first hit" = firstTransform - "2ndHit = 2ndTransform --> if (firstTransform = 2ndTransform) ??
That doesn't look really elegant to me
Hi there and really sorry for my late reply. Yes once you get the 2 tranforms you can do if (firstTransform == secondTransform)
The reason is that if the objects hits are in fact only 1 object, then their transform is the same object in memory and you can use ==
But in this case I have to change my check method for a raycasthit :/
Any chance to get the transform "hit" by overlapcircle ?
OverlapCircle returns a Collider2D. You can compare directly the 2 Collider2D objects that you get. (RayCast returns an object containing information about the point of contact of the ray which is why you can't compare them directly, but OverlapCircle just returns the collider it collided).
If you have problems because the OverlapCircle hits several colliders and therefore doesn't return the ground collider, then you need to use OverlapCircleAll and find which collider corresponds to the ground in the array of Collider2D.
Sounds cool ! I'll try that out as soon as i come back from my 3days trip :)
Thx again !
Answer by ninja_gear · May 27, 2016 at 03:20 AM
You might try putting a bool CollidingWithPlayer into the ground tile, and then when a collision occurs check to see if its the player, if it is set to true. then, if another collision occurs while that property is true, you can compare the new collision, dismiss if its just the player colliding again, and if not, then presto.
Thanks but i'd prefer to have this script on my characters objects rather than on every grounds :)
Your answer
Follow this Question
Related Questions
3d map of all colliders in scene 1 Answer
Best method to make the main character traverse multiple layers of objects using Unity 2D 1 Answer
How can I let the ray to the edge of the capsule? 1 Answer
How to detect if the player object is on top of a moving platform? 1 Answer
2D collision won't work? 2 Answers