Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by RemDust · May 25, 2016 at 07:33 AM · colliderscollision detectionraycastingoverlapsphere

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 !

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RemDust · May 25, 2016 at 02:08 PM 0
Share

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

avatar image GiyomuGames RemDust · May 27, 2016 at 01:09 AM 0
Share

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 ==

avatar image RemDust · May 27, 2016 at 08:44 AM 0
Share

Great then ! So trivial but efficient ^^

Thanks man

avatar image RemDust · May 27, 2016 at 08:47 AM 0
Share

But in this case I have to change my check method for a raycasthit :/

Any chance to get the transform "hit" by overlapcircle ?

avatar image GiyomuGames RemDust · May 27, 2016 at 08:51 AM 0
Share

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.

avatar image RemDust GiyomuGames · May 27, 2016 at 09:28 AM 0
Share

Sounds cool ! I'll try that out as soon as i come back from my 3days trip :)

Thx again !

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RemDust · May 27, 2016 at 08:43 AM 0
Share

Thanks but i'd prefer to have this script on my characters objects rather than on every grounds :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

46 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges