- Home /
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
Answer by zmar0519 · Apr 01, 2011 at 01:41 AM
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.
Answer by Justin Warner · Apr 01, 2011 at 02:27 AM
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!
good idea for a single change but wouldn't work with applying multiple bullet holes (or paint slashes in my case)
Thanks for the tip though
Your answer
Follow this Question
Related Questions
Bullet Shoot with RayCast don't destroy onCollisionEnter 1 Answer
How do I allow an object pass through a wall/object of the same colour/material? 2 Answers
How to destroy bullet on collision! 2 Answers
Collision misses - possibly not bullet through paper! (now with video) 1 Answer
Bullet Holes, Decals 0 Answers