Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 direcalmly · Aug 27, 2014 at 02:45 AM · 2dcolliderplatformerdynamiccreation

Dynamically Created Edge Collider Woes

Hey all

I'm creating a 2D platformer with tile-based levels. Each tile is 6x6 units, square. I'm working on a script right now that detects neighboring tiles and only generates edge colliders for "open" edges. So if a tile is right next to another tile, they'll detect each other and only place edge colliders on the edges that aren't touching. Hopefully that makes sense. I've got the script to work to make the edge colliders by firing four raycasts (top, bottom, left, right) which collide against temporary boxcollider2D's attached to the tiles.

 hitT = Physics2D.Raycast (new Vector2(transform.position.x, transform.position.y + 3.25f), transform.up, 0.25f, 1 << LayerMask.NameToLayer("Ground"));
 
 if (hitT){}
             else
             {
                 EdgeCollider2D eCT = gameObject.AddComponent<EdgeCollider2D> ();
 
                 eCT.points = new Vector2[] 
                 {
                     new Vector2(-3, 3),
                     new Vector2(3, 3)
                 };
             
                 eCT.sharedMaterial = Resources.Load ("Materials/Ground") as PhysicsMaterial2D;
             }

This is the code just for the top edge collider, the other three are redundant. So this works fine in creating the edge colliders, but there seems to be something wrong with the physics on the tiles. The material "Ground" has 0 friction and 0 bounciness, so when my player contacts it with any motion at all he should just slide across it, however... he just sticks to it and can't move.

The issue is that the collider is behaving as though the physicsmaterial2d isn't loaded even though I can see it is in the inspector. Oddly enough, if I'm in play mode I can simply select the tiles that aren't working and reload the physics material and it will function fine. Is this a bug or am I simply not loading the material correctly?

EDIT: Found a workaround. Not sure why, but disabling and re-enabling the collider makes the change take effect. New code:

         if (hitT){}
         else
         {
             EdgeCollider2D eCT = gameObject.AddComponent<EdgeCollider2D> ();
 
             eCT.points = new Vector2[] 
             {
                 new Vector2(-3, 3),
                 new Vector2(3, 3)
             };
         
             eCT.sharedMaterial = Resources.Load ("Materials/Ground") as PhysicsMaterial2D;
             eCT.enabled = false;
             eCT.enabled = true;
         }

Hopefully this will help someone out.

Comment
Add comment · Show 1
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 Ev · Dec 25, 2014 at 10:16 PM 0
Share

Thanks. Very helpful.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

physics materials not showing up? 1 Answer

How to be dynamically attached to the PolygonCollider2D in on iOS 1 Answer

[Answered] Level Complete? 1 Answer

Tile-based approach in Unity 1 Answer

Change gravity on button 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