- Home /
Swap Mesh upon Collision?
Hello everyone! I'm making a modified roll-a-ball game for class, and I've run into a bit of trouble with one of the features I wanted to include. Instead of making meshes disappear upon collision, I want that mesh to change into something else. It would still behave the same as the previous mesh, and be of a similar size, it would just look different.
I'm not sure how how to set up or trigger this change. Any tutorials or advice?
Much appreciated!
Answer by Flickayy · Aug 15, 2015 at 09:09 AM
You'll want to take a look at the MeshFilter component for this kinda thing.
From this, you can then call MeshFilter.mesh.
By using mesh property you can modify the mesh for a single object only. The other objects that used the same mesh will not be modified.
Can't really give an example right now, but I'll come back and update this post later after I've woken up, hope this info helps.
Answer by DrakoRex11 · Aug 15, 2015 at 02:09 PM
I am going to guess that you are using unity's physics engine?
try this.
//up top above all of your methods(but before your header)
public mesh mesh1;
//2,3, etc as many as you want.
//just copy that line and rename
int state = 0;
int SMAX = 2;//maximum number of meshes (changeable)
void OnCollisionEnter (Collision collision) {
if(state == 0){this.gameObject.GetComponent<MeshFilter>().mesh = mesh1;}
//else if(state == 1){this.etc add mesh 2 here.
if(state < SMAX){state++;}
else{state = 0}
}