- Home /
The question is answered, right answer was accepted
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!
The gameObject you are setting the layer have any collider?
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.
$$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.
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.
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.
Follow this Question
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