Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Generating a collision mesh at runtime

Discussion in 'Scripting' started by Murgilod, Mar 20, 2014.

  1. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    9,714
    I'm attempting to make a game where the player can fire a Special Gun™ that allows them to create spherical holes in the ground. So far I have a shader that accommodates this, and it's pretty spiffy.





    However, I want to make it so that when the player does this, the collision area in the hole will basically not exist.



    So, my plan was to use the world position of the centre of the sphere (or maybe a spherecast) to find the collision data relative to the centre of the sphere, but extended outward slightly, generate a new collision mesh by subtracting a low-poly (240 tri or so) sphere from the mesh area, and then switch the player to that collision layer when they enter a trigger collider slightly bigger than this. So I have a few questions.

    1. Would the spherecast or world position calculations be faster?
    2. How do I do the calculations to subtract the sphere from the collision geometry in the fastest way possible? Accuracy and mesh quality are not terribly important, as it's just for collision, not rendering.
    3. Is it possible to do this fast enough to keep up with 16 players basically doing the same thing?
     
  2. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    9,714
    Anyone?
     
  3. Cynikal

    Cynikal

    Joined:
    Oct 29, 2012
    Posts:
    122
    A easy solution would be, on your sphere collider, use it as a trigger, and OnTrigger Enter, Disable the "grounds" collider... In turn causing your player to fall... Then when you exit t he collider...reenable it.
     
  4. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    9,714
    This presents far too many issues.

    1. Any intersection with the trigger would cause the player to disconnect with the geometry, causing them to either get stuck in, or ejected randomly from the geometry they exit in.
    2. The player can no longer interact with the slopes that would be generated in a new collision mesh.
    3. Even with a two trigger system, both of these issues would persist, making gameplay too unreliable for multiplayer.
     
  5. TSnake

    TSnake

    Joined:
    Mar 12, 2014
    Posts:
    13
    You can use Physics.IgnoreCollision for Holes. With this script, add your "Colider of the terrain" in Voidable, and set your hole collider to Trigger and try.

    Code (CSharp):
    1.    
    2. public Collider[] Voidable;
    3.     void OnTriggerEnter(Collider col)
    4.     {
    5.  
    6.         for (int i = 0; i < Voidable.Length; i++)
    7.         {
    8.             Physics.IgnoreCollision(Voidable[i].collider, col, true);
    9.         }
    10.     }
    11.     void OnTriggerExit(Collider col)
    12.     {
    13.         for (int i = 0; i < Voidable.Length; i++)
    14.         {
    15.             Physics.IgnoreCollision(Voidable[i].collider, col, false);
    16.         }
    17.  
    18.     }
    Test :
    http://i.imgur.com/l1e9njo.png
    http://i.imgur.com/qepqBTU.png
    http://i.imgur.com/vgpwCHS.png