- Home /
Platforms lose their frictionless property after dynamic layer change
TL;DR at the bottom!
So I've created a basic puzzle game with simple mechanics. The two core mechanics are:
if a player has neutral color (let's say white for example), he/she acquires the color of any colored objects he/she collides with
when a player collides with a platform, it changes color to match that of the player.
This color change is put into effect by a sprite and layer change. I used layers to store the current color of all gameObjects to make use of the Layer Collision Matrix (objects can only collide with objects of the same color, i.e. if a player is blue he/she cannot collide with a red platform). All platforms (both vertical and horizontal) have a Platform Effector 2D component to make use of the "frictionless sides" property.
The frictionless sides works perfectly as long as the player is neutral colored. Once a player acquires a color, any platform he/she touches just loses its frictionless sides and gets the classic "sticky sides" bug where if you continually hold the arrow key in the direction of the platform you'll just stay stuck there without falling. I've got no idea why this happens and I've tried/checked everything I can think of to attempt to fix it to no avail. Any help is super appreciated, thanks!
Here is the code I've got for the player to update color/sprite: (Layers 8, 9, 10 are Neutral, Blue, and Red respectively and I used OnCollisionStay as well because the player can press a button to drop the current color and if the player is on a platform when the color is dropped they should pick it up again without having to re-collide)
void OnCollisionEnter2D(Collision2D collision) {
float positionY = transform.position.y;
float collisionPosY = collision.GetContact(0).point.y;
int layer = collision.gameObject.layer;
// Color Update
if (collision.gameObject.layer == 9) {
GetComponent<SpriteRenderer>().sprite = blue;
gameObject.layer = 9;
} else if (collision.gameObject.layer == 10) {
GetComponent<SpriteRenderer>().sprite = red;
gameObject.layer = 10;
}
}
void OnCollisionStay2D(Collision2D collision) {
// Continuous Color Update
if (collision.gameObject.layer == 9) {
GetComponent<SpriteRenderer>().sprite = blue;
} else if (collision.gameObject.layer == 10) {
GetComponent<SpriteRenderer>().sprite = red;
}
}
And here's the code for the platforms:
void OnCollisionEnter2D(Collision2D collider) {
// only change color if neutral
if (gameObject.layer == 8) {
if (collider.gameObject.layer == 9) {
GetComponent<SpriteRenderer>().sprite = blue;
gameObject.layer = 9;
} else if (collider.gameObject.layer == 10) {
GetComponent<SpriteRenderer>().sprite = red;
gameObject.layer = 10;
}
}
}
Here is a list of all the things I've checked to find the bug:
if the player sprite change impacted the player collision detection
if removing OnCollisionStay would solve the problem
if removing/changing the arc/rotational offset of the platform effector would solve the problem
if the platforms' or player's color change impacted the frictionless property
From my testing as well as isolated tests on this one issue, I've found that the bug only occurs whenever either of the objects in a collision has a layer value that is NOT neutral, so either blue or red
TL;DR:
I want platforms to be frictionless on the sides, so I'm using Platform Effector2D. Whenever I change the layer of a platform it loses its frictionless property.
Your answer
Follow this Question
Related Questions
2D Moving platforms is slippery! 3 Answers
Dynamically add layers 1 Answer
Dynamic Layer Switching Performance 0 Answers
Console prints message multiple times 1 Answer
Sprites render before UI when starting game first time 1 Answer