Change physics material during game when the player hits an object

In this tutorial i will show you How to change physical material during game when the player hits an object or make collision with in unity . by using c# code :


  1. How to Add a Physics Material to 3D Object:

  • add a Rigidbody to you 3D object .
  • In Project Layout create a new Physics Material:


  • Change details of the Physics Material:


  • put this Physics Material in the collider of the 3D Object:

2 . Change physics material during game when the player hits an object?

  • Exemple for Start() Method :

  1. using UnityEngine;
     using System.Collections;
     
     public class ExampleClass : MonoBehaviour {
         public float dynFriction;
         public float statFriction;
         public Collider coll;
         void Start() {
             coll = GetComponent<Collider>();
             coll.material.dynamicFriction = dynFriction;
             coll.material.staticFriction = statFriction;
         }
     }

  • Exemple 2 :

public PhysicMaterial physicMaterial;
 
 public void OnSomething()
 {
     GetComponent<Collider>().material = physicMaterial;
 }


unity collision detection,unity collision trigger,unity 3d,unity box collider,game development,game development for beginners,collider,unity physics,unity3d physics,oncollisionenter,ontriggerenter,rigidbody