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 coolblue2000 · Mar 04, 2017 at 11:26 PM · 2d game

IsTouchingLayers() is not working

I have a 2D game with a handful kinematic objects.

I am checking in OnTriggerEnter2D for IsTouchingLayers but it is acting very odd

This is the Code

 private void OnTriggerEnter2D(Collider2D collision)
     {
             int p=collision.gameObject.layer;
             int r = LayerMask.NameToLayer("Ships");
             if (collision.IsTouchingLayers(LayerMask.GetMask(new string[] { "Ships" })))
             //if(p==r)
             {
                 Destroy(this.gameObject);
             }
     }

So this code does not work as it is, the Orb (which this script is attached to) is on layer "Items" and the ship (which is what it has collided with is on layer "Ships".

However if I change the ship to layer "Items" (and obviously change the code to match) then IsTouchingLayers does indeed then work. But it does not work if the ship is on any other layer. So IsTouchingLayers only appears to be working for objects on the same layer...

If I comment out the IsTouchingLayers If statement and uncomment the p==r If statement then the code works fine.

The Collision Matrix has all boxes ticked.

I am using version 5.5

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
1
Best Answer

Answer by Commoble · Mar 04, 2017 at 11:58 PM

The collision/collider parameter in all collision/trigger events always refers to the other object involved in the collision/trigger. Since your script above is on the Orb, let's look at what your variables in the script are referencing when something collides with an Orb:

int p=collision.gameObject.layer;

This is the layer index of the other object, potentially a Ship.

int r = LayerMask.NameToLayer("Ships");

This is the layer index of the Ship layer.

if (collision.IsTouchingLayers(LayerMask.GetMask(new string[] { "Ships" })))

Now, in this one up there, you're checking if the other object (potentially a Ship) is touching an object whose layer is the Ship layer,

if(p==r)

whereas in that one, you're checking if the other object's layer is the Ship layer.

So you see why the one doesn't work and the other does?

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 coolblue2000 · Mar 05, 2017 at 12:18 AM 0
Share

Wow! How on earth did I not spot that obvious logic...

Thank you for spotting that glaring error for me.

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

68 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 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 do I make the scrolling texture background change direction depending on keyboard input for the player movement controls? 3 Answers

Smooth Top Down Camera (2D) 0 Answers

How to use 3D model on 2D Game with collider and Gravity? Please give me some Guideline. 0 Answers

Collision with the ground kills the player using c#(Unity2D) 2 Answers

2D repeating obstacles with a random Y value 0 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