Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by MapuHoB · Jan 01, 2015 at 02:41 PM · 2dcollisionoverlappinglayering

Overlapping colliders problem 2d

Many times I have 2 GOs one over another, different sorting layer, and I click over the top(the one with the higher sorting layer) but it doesn't find collision with it but with the one behind. So my question is how can I fix it without deactivating the colliders of the rear GO?

Simple example- http://prntscr.com/5n948m

so in this case for example if I click on the button "R" sometimes finds collision with the platform not the button and the platform is in sorting layer 1 and the button 2, but still if I deactivate the collider of the platform, the player is going to fall through it.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by MapuHoB · Jan 06, 2015 at 03:44 AM

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System;
 
 public enum SortingLayers { FirstLayer, SecondLayer, ThirdLayer, Fourth, RunTimeMenu, BlackScreen, GameIntro}
 
 public class SortingLayerManager : MonoBehaviour {
     private static Dictionary<string, int> SortingLayers = new Dictionary<string,int>(10);
     
     void OnEnable()
     {
         SortingLayers.Add("FirstLayer", 0);
         SortingLayers.Add("SecondLayer", 1);
         SortingLayers.Add("ThirdLayer", 2);
         SortingLayers.Add("FourthLayer", 3);
         SortingLayers.Add("RunTimeMenu", 4);
         SortingLayers.Add("BlackScreen", 5);
         SortingLayers.Add("GameIntro", 6);
     }
 
     private static int Compare(GameObject gameObject1, GameObject gameObject2)
     {
         string layer1 = gameObject1.renderer.sortingLayerName,
                layer2 = gameObject1.renderer.sortingLayerName;
 
         if (SortingLayers[layer1].CompareTo(SortingLayers[layer2]) == 0)
         {
             return gameObject1.renderer.sortingOrder.CompareTo(gameObject2.renderer.sortingOrder);
         }
 
         return SortingLayers[layer1].CompareTo(SortingLayers[layer2]);
     }
 
     public static bool IsTopmost(GameObject go)
     {
         RaycastHit2D[] hits;
         Vector3 wp = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         wp.z = Camera.main.transform.position.z;
         hits = Physics2D.RaycastAll(wp, Vector2.zero, Vector3.Distance(Camera.main.transform.position, go.transform.position) * 2);
 
         if (hits.Length == 0)
         {
             return false;
         }
 
         GameObject topMostSoFar = hits[0].collider.gameObject;
 
         RaycastHit2D hit;
         for (int i = 1; i < hits.Length; i++)
         {
             hit = hits[i];
 
             if (Compare(topMostSoFar, hit.collider.gameObject) == -1)
             {
                 topMostSoFar = hit.collider.gameObject;
             }
         }
 
         return topMostSoFar == go;
     }
 }


I came up on my own with this solution, and whenever I check for click I call the IsTopmost method and if it is then I do the logic.

Comment
Add comment · 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

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

25 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

Related Questions

How can I prevend two Circle Collider's (2D) overlapping on collision? 1 Answer

Collision problem 0 Answers

2D box colliders not touching but are colliding, how to fix? 0 Answers

Weapon System with collide detection (Helps with script pls)!!! 0 Answers

Collision vs trigger 1 Answer


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