Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by josak46 · Feb 16, 2016 at 01:00 PM · javascriptontriggerenterphysicmaterialbounciness

How to change physic material during game when the player hits an object?

I'd like to change bounciness in Physics material with a script. So when player hits gameobject bouncines changes for some %. Until now I've made a script that doesn't work. The bounciness stays the same when player collides with game object. Also my script would only change bounciness to 0, so if anyone has an idea how to change bounciness for few % every time player hits this object please do not hesitate to inform me :) Here is the script: (I'm a self-taught beginner so please be understanding :) )

 var PlayerColl: Collider;
 var material: PhysicMaterial;
 var dynamicFriction=2;
 var target:Transform;
 var col : Collider;
  
  
 function Start() {
     PlayerColl = GetComponent.<Collider>();
    
     material = new PhysicMaterial();
     material.dynamicFriction = dynamicFriction;
     material.bounciness = 1;
     PlayerColl.material = material;
     }
  
 function OnTriggerEnter(col : Collider)
 {
     if(col.transform.name == "Player")
      {
  
     material.bounciness = 0;
    
    
 }
 }

i also tried with this but still no effect..

  function OnTriggerEnter (col : Collider){
     
       if (col.transform.name == ("Player")){
             PlayerColl = GetComponent.<Collider>();
         material = PhysicMaterial();
         material.dynamicFriction = dynamicFriction;
         material.bounciness = 0;
         PlayerColl.material = material;
       
       }
     
     
     }

Thank you for your time..

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by FelixTwoMenAndADog · Jun 30, 2016 at 02:32 PM

You can also change the material values directly, but keep in mind that they will change for every object using them.

Though I encountered a bug where you have to disable and enable the collider for the new values to take effect.

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 Jim_the_Game_Guy · Oct 27, 2021 at 09:29 AM 0
Share

Thank you for mentioning this bug. I added this (disable an enable collider) to my code that changes a physics material property, and it went right to work! I spent 2 hours trying everything yesterday. Thank you!

avatar image
1

Answer by GioVil · Feb 16, 2016 at 04:26 PM

Have you tried to set the bounciness in the component directly without to create a new material?

Comment
Add comment · Show 4 · 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 josak46 · Feb 16, 2016 at 09:05 PM 0
Share

Do you mean if I tried to create material in game and than do a script that changes this material in game? Yes I did but it doesn't work..the same as with my example..bounciness doesn't change..

avatar image josak46 · Feb 16, 2016 at 09:52 PM 0
Share

@GioVil I tried it again..to make a material in game and add it to the olayer collider and than try to change it with a script. So now my script looks like this..but the script wants to change material to object that the player colides with and not the player's.

$$anonymous$$issingComponentException: There is no 'SphereCollider' attached to the "DecreseBouncines" game object, but a script is trying to access it. You probably need to add a SphereCollider to the game object "DecreseBouncines". Or your script needs to check if the component is attached before using it. 2ball.Update () (at Assets/2ball.js:15)

 var PlayerColl: SphereCollider;
 var material: Physic$$anonymous$$aterial;
 var target:Transform;
 var col : Collider;
 
 
     function OnTriggerEnter(col : Collider)
      {
         if(col.transform.name == "Player")
          {
         PlayerColl = GetComponent.<SphereCollider>();
         material = Physic$$anonymous$$aterial();
         material.dynamicFriction = 2;
         material.bounciness = 0;
         PlayerColl.material = material;
         }
      }






avatar image martipello · Feb 16, 2016 at 10:21 PM 1
Share

Is this script attached to player?

avatar image josak46 martipello · Feb 17, 2016 at 11:08 AM 0
Share

@martipello If the script is attached to the player or to empty object, the collision is not detected.

avatar image
0

Answer by nogotogon · Nov 26, 2018 at 12:26 PM

I am trying to write something similar, and Am getting it to change, but the changes only go into affect when the scene reloads. Do you have any idea how to have the game update / reload an object or material without resetting the scene?

Comment
Add comment · 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

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Need help with OnTriggerEnter 3 Answers

Enable/ Activate a script by script 1 Answer

Don'tDestroyOnLoad issue, please help :( 1 Answer

Object reference not set to an instance of an object, How can I fix it? 0 Answers

How do I add force in the on trigger 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