Search Unity

swapping meshes at runtime?

Discussion in 'Editor & General Support' started by techbinge, Dec 6, 2006.

  1. techbinge

    techbinge

    Joined:
    Jul 22, 2006
    Posts:
    88
    Is it possible to swap meshes in / out at run time? Say for damage models, or even just completely replacing one w/ another?
     
  2. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    Easily, in fact. The MeshFilter component takes a Mesh. So you can just swap this at runtime to whatever you want by a script. The same should be true for MeshCollider.

    http://unity3d.com/Documentation/ScriptReference/MeshFilter.html

    For example (untested):

    Code (csharp):
    1.  
    2. var damagedMesh : Mesh;
    3.  
    4. function OnCollisionEnter()
    5. {
    6.    (gameObject.GetComponent(MeshFilter)).mesh = damagedMesh;
    7. }
    8.  
    That will set the mesh to what you assign to damagedMesh when the collider hits something. You can make it more advanced from there say by only switching the mesh when the collision velocity is above a certain value.

    Hope that helps,
    -Jon
     
  3. techbinge

    techbinge

    Joined:
    Jul 22, 2006
    Posts:
    88
    awesome, I'll have to play w/ that! Thank you!
     
  4. David-Helgason

    David-Helgason

    Moderator

    Joined:
    Mar 29, 2005
    Posts:
    1,104
    Or you might want to instantiate an object and destroy the current one. The object you instantiate could have a particle system (or any other effects on it).

    d.
     
    Gyromight likes this.