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 Bofoi · Mar 28, 2019 at 03:58 PM · layersdynamicfrictionplatforms

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.

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

0 Replies

· Add your reply
  • Sort: 

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

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

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


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