add material to object when a collision occurs

Hi all,

Have been searching around for ages but struggling to add a material to an object when a collision occurs - like a bullet hole on a wall. For the projectile I have this

  var splatMat:Material;

  function OnCollisionEnter (collisionInfo : Collision) {

    // Rotate the object so that the y-axis faces along the normal of the surface
    var contact : ContactPoint = collisionInfo.contacts[0];
    var rot : Quaternion = Quaternion.FromToRotation(-Vector3.right, contact.normal);
    var pos : Vector3 = contact.point;
    var mats=collisionInfo.gameObject.renderer.materials;

    /* Question here *****/

//how do I add my splatMat to the materials object on the collisionInfo.gameObject?

    // Destroy the projectile
    Destroy (gameObject);

  }

thanks in advance

Are you looking to purchase the easy save asset on the asset store? If so: https://www.assetstore.unity3d.com/en/#!/content/768

Also, for what it's worth. This post is almost 2 years old. I've since started using this asset which has worked out really well: https://www.assetstore.unity3d.com/en/#!/content/36879

2 Answers

2

My advice is to simply use a cube with a scale of 1, 1, 0.05 with your texture/material already on it and instantiate it at the hit point. see also the gun script in the bootcamp demo.

Alright, kind of what Joe said, so +1.

But here's the thing. Game objects can't have their material changed at runtime (The renderer is read only according to their API)...

So, to get around this...

You have 2 of the game objects.

On collision, you get the position of the model, instantiate the game object with the new material, and then get the game object of the collider, and destroy the actual model... Then it gives the illusion that it just changed, but really it's a new game object all together =).

Hope this helps =). Good luck!