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 /
This question was closed Dec 11, 2017 at 10:24 PM by burakgo for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by burakgo · Dec 11, 2017 at 10:49 AM · 2d gamelayers2d platformer2d collisionlayercollision

Changing layers on runtime does not work

Hello!

I am making a simple 2D platformer runner game like Fun Run. I want the player to go through walls for a second when he respawns. To implement this feature, I added an extra layer (GhostPlayer) to the game. Player is normally in Player layer which collides with everything but in GhostPlayer Layer it only collides with the frame and not with the obstacles on the road.

Both layers are working as intended, if I change the layer and then play. However, if I change the layer in runtime using
gameObject.layer = LayerMask.NameToLayer("GhostPlayer");
player keeps colliding to the obstacles even though I can see in the inspector that I successfully changed the layer.
I do not want to disable the collider2D since then player falls down. And I will also use this method to implement a Dash skill to dash forward a distance where player will not collide with obstacles but will be able to get coins or buffs on the way. That said, any other way to implement this is welcome.
Thank you in advance!

alt text

alt text

player1inspector.png (62.5 kB)
physics2dsettings.png (33.8 kB)
Comment
Add comment · Show 6
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 lassade · Dec 11, 2017 at 07:09 PM 0
Share

The gameObject you are setting the layer have any collider?

avatar image burakgo · Dec 11, 2017 at 07:55 PM 0
Share

Yes, it has box collider 2D. I also tested that, if I change the layer at Start or Awake it works as intended. However, doing so in any of updates does not work.

avatar image fdz_ burakgo · Dec 11, 2017 at 08:31 PM 0
Share

$$anonymous$$aybe the code in Update it's not right? I mean. From what you said, all should be right, but, just incase:

  • Are the physics setted propertly? Edit > Project Settings > Physics 2D

  • Like lassade said, The gameObject you are setting the layer have any collider? and that gameObject is the one that has the script? Since gameObject.layer will not set the layer of the childs.


You could post the code.

avatar image burakgo fdz_ · Dec 11, 2017 at 09:01 PM 0
Share

I know physics settings are correct because if the player's layer is GhostPlayer when i start the game, it works as intended.
I also know that the code is working cause it changes the layer on the inspector and if I change the layer via code in Start or Awake with the same code

(gameObject.layer = Layer$$anonymous$$ask.NameToLayer("GhostPlayer");)
it works. However, I could not manage to work it in updates, with or without an input.
gameObject has the one that has the script. It has only one child which is just a sprite with no collider or anything else.
Also, this is my first question here and I do not know if I should post the 230 line (350 line with the inherited class) right here. Other than that, I have already posted the line of code that does the work.

Show more comments

1 Reply

  • Sort: 
avatar image
2
Best Answer

Answer by burakgo · Dec 11, 2017 at 10:24 PM

All right, I have found my mistake.
I did get just one part of the project from a tutorial(2D movement code), and apparently, that code uses this;

  void Start()
     {
         contactFilter.useTriggers = false;
         contactFilter.SetLayerMask(Physics2D.GetLayerCollisionMask(gameObject.layer));
         contactFilter.useLayerMask = true;
     }

It is really my fault not to check that part before coming here. I am not yet sure how ContactFilter2D works yet but when I change this part

 gameObject.layer = LayerMask.NameToLayer("GhostPlayer");

to

     gameObject.layer = LayerMask.NameToLayer("GhostPlayer");
     contactFilter.SetLayerMask(Physics2D.GetLayerCollisionMask(gameObject.layer));
     contactFilter.useLayerMask = true;

It works as intended.

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

Follow this Question

Answers Answers and Comments

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

,Unity Spieler Gravity funktioniert nicht? 1 Answer

Collision and layering for 2D top down project with hand drawn backgrounds 0 Answers

how to avoid collision between first and third layer when second is between them? 2D 0 Answers

Why didnt my Collider works? 0 Answers

Laggy behaviour after two object with colliders are generated on the same place 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