- Home /
 
Changing physics properties of a gameobject through code.
I have a sphere in this little game i'm building, and sometimes its a bouncy ball and sometimes its a metal ball. The bouncy ball works perfect but when I change to metal ball and ball is in the air it bounces too. I have the physics materials set. I also put a physics material on platform that has no bouciness. The only thing that works is a surface effector that doesn't use bounce. I dont want to change the bool on all surface effectors and I don't want surface effectors on everything. Here is a piece of my code, I change properties with a button.
P.S. This is a 2D game.
 public void HeavyBall()
 {
     surEffect.useBounce = false;
     rend.sharedMaterial = solid;
     col.sharedMaterial = heavyBall;
     rigidBody.gravityScale = 3f;
     rigidBody.drag = 0.2f;
     rigidBody.angularDrag = 0.5f;
 }
 public void RubberMaterial()
 {
     surEffect.useBounce = true;
     rend.sharedMaterial = rubber;
     col.sharedMaterial = bouncingRubber;
     rigidBody.gravityScale = 1f;
     rigidBody.drag = 0f;
     rigidBody.angularDrag = 0.05f;
     if(isOnGround)
     {
         rigidBody.velocity = new Vector2( 0f, 7.5f);
     }
 }
 
              Hi,
I'm having the same issue in my current project. I'm not 100% sure, but it seems to me that bounciness and friction values are somehow swapped. It's when I bring friction to 0 that the rigid body stops bouncing.
Did you end up finding a solution to this?
LOL if this is the case it'll be the find of the century :P I've been plagued by see$$anonymous$$gly random bounciness for ages
Answer by wibble82 · Dec 03, 2015 at 02:16 PM
Hi there
I could be wrong but I believe you should be changing the material of your collider, not the sharedMaterial. sharedMaterial is the base material that your collider's material is created from - it's a bit like the prefab for its physics material. The material that it is actually using is collider.material.
See definitions of material and sharedMaterial for collider:
http://docs.unity3d.com/ScriptReference/Collider.html
Also not sure what this bit does:
  if(isOnGround)
      {
          rigidBody.velocity = new Vector2( 0f, 7.5f);
      }
 
               That strikes me as a curious bit of code - probably not the problem, but still odd!
-Chris
Your answer