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 briveramelo · Aug 25, 2014 at 04:32 AM · frictionsharedmaterialbounciness

How can I edit a SINGLE collider2D's sharedMaterial properties (friction, bounciness) during runtime without changing it for everything else with the same sharedMaterial?

I have a 2D platformer. In it, there's a weapon that freezes things for a fixed amount of time, so it changes the friction properties for everything close by. The problem is, I have items far away from it that are also being affected because they share the same sharedMaterial. I have read that this is not a problem with the 3D equivalent "Collider" and "material", but I need to stick to "Collider2D", and I would rather not make/reference 1000 distinct sharedMaterials. Thoughts? I appreciate any ideas.


Thank you for the clarifying question.

I want the friction to be a function of distance from the point of contact and starting friction. So imagine the freeze weapon hits a spot at (0,0), there's an item A at (0,1) and another item B with the same sharedMaterial at (0,2). I want A to be more slippery than B for like 5 seconds, and then return to normal.

Any new thoughts now? It's kinda tricky.


First post here on the forums too, so please give me a virtual slap if I'm doing something out of the forum's social canon.

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 Slobdell · Aug 25, 2014 at 04:52 AM 0
Share

Why do you need 1000 distinct materials? Can't you just have one for not frozen and one for frozen?

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Tomer-Barkan · Aug 26, 2014 at 01:55 AM

sharedMaterial is just that, it's a material shared by many objects (all instances of the same prefab by default use the same material). Changing it would change all those objects.

In 3d, you have a material property, for some reason they didn't put it in 2d. But it is simple to simulate the same behaviour. What happens is when you access collider.material, it creates a new material, a copy of the sharedMaterial, attaches it to the collider, and then returns it. So in your script, you could use these lines to do just that (Haven't tried it myself though):

 PhysicsMaterial2D newMaterial = Instantiate(collider2d.sharedMaterial);
 collider2d.sharedMaterial = newMaterial;

now whatever you change in the sharedMaterial will only affect this one.

BUT - this way you might create many 1000 physics materials like you said. So what you have to do, is to create a cache for all those materials that have the same properties. use this code:

 collider2d.sharedMaterial = GetMaterial(friction); // friction is the amount of friction you calculated that is required


 private static Dictionary<float, PhysicsMaterial2D> materialCache = new Dictionary<float, PhysicsMaterial2D>();

 private static PhysicsMaterial2D GetMaterial(float friction) {
     if (!materialCache.ContainsKey(friction)) {
         materialsCache[friction] = Instantiate(collider2d.sharedMaterial);
         materialsCache[friction].friction = friction;
     }
     return materialsCache[friction];
 }

This will only create one material per friction, and reuse it to all objects that require that kind of friction.

Note: you might want to clear the cache when necessary or use a smarter cache, since I used static and it never clears.

Comment
Add comment · Show 1 · 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
avatar image briveramelo · Aug 26, 2014 at 03:34 PM 0
Share

thanks bud! that worked for me (after a little tweaking). Quite a crafty solution, I must say, and cleverly deduced on your part through a solid understanding of the 3D engine. I also had to make sure I disabled and enabled the collider2D before and after changing the physicsmaterial2D (because that's a known bug). Thank you, kind sir, may you be showered with good fortune!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How can I make different physic materials on one 3d object to make different frictions and bounciness? 1 Answer

Elastic Collisions with Rigidbodies 1 Answer

Platform effector bounciness 0 Answers

Wheel Collider General Friction 1 Answer

About physic calculation of wheelcollider 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