Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Help With Fragmented Object Colliders

Discussion in 'Scripting' started by NeatMaddness, Oct 4, 2015.

  1. NeatMaddness

    NeatMaddness

    Joined:
    Nov 14, 2014
    Posts:
    30
    I am looking for some help with my fragmented objects that I created in Blender and imported into Unity. I have everything pretty much working how I want it. When the asteroid is shot is blows up into pieces. My problem is that I would like to turn off the colliders on those little pieces so they dont result in a "Game Over" when hit.

    Here is a video showcasing my game and my exact problem:


    I want to turn off the colliders but dont know how to without turning off the rest of the colliders on the other asteroids in the scene. I just want to disable the colliders on the asteroid thats hit. Anyone have an idea on how to do this?

    This is my asteroid script:
    Code (CSharp):
    1. public class FragRock : MonoBehaviour {
    2.  
    3.     private float rockTime = 5.0f;
    4.    
    5.     public LevelManager _LevelManager;
    6.     public BulletDestroy _BulletDestroy;
    7.    
    8.     public float sphereRadius;
    9.     public LayerMask whatIsObstacle;
    10.    
    11.    
    12.     private float radius = 10.0F;
    13.     private float power = 30.0F;
    14.    
    15.    
    16.     void Start()
    17.     {
    18.         _LevelManager = GameObject.FindObjectOfType<LevelManager>();
    19.     }
    20.    
    21.     void OnTriggerEnter(Collider other)
    22.     {
    23.         if(other.gameObject.tag == "points")
    24.             Destroy(this.gameObject);
    25.        
    26.         if(other.gameObject.tag == "bullet")
    27.         {
    28.             Destroy(this.gameObject);
    29.         }
    30.         if(other.gameObject.tag == "ship")
    31.         {
    32.             _LevelManager.GameOver();
    33.         }
    34.     }
    35.    
    36.     void FixedUpdate()
    37.     {
    38.        
    39.         if(Physics.CheckSphere(transform.position, sphereRadius,whatIsObstacle))
    40.         {
    41.             Vector3 explosionPos = transform.position;
    42.             Collider[] colliders = Physics.OverlapSphere(explosionPos, radius);
    43.             foreach (Collider hit in colliders)
    44.             {
    45.                 Rigidbody rb = hit.GetComponent<Rigidbody>();
    46.                
    47.                 if (rb != null)
    48.                     rb.AddExplosionForce(power, explosionPos, radius, 0F);
    49.             }
    50.            
    51.             _BulletDestroy = GameObject.FindObjectOfType<BulletDestroy>();
    52.             _BulletDestroy.DestroyShot();
    53.         }
    54.     }
    55.    
    56.    
    57.     void Update ()
    58.     {
    59.         rockTime -= Time.deltaTime;
    60.        
    61.         if(rockTime <= 0.0f)
    62.         {
    63.             Destroy (this.gameObject);
    64.         }
    65.        
    66.     }
    67.    
    68. }
    69.  
    Thanks!
     
  2. NeatMaddness

    NeatMaddness

    Joined:
    Nov 14, 2014
    Posts:
    30
    Ok so I went with a different idea and just reduced the fragments of each asteroid by a lot (now only 5 fragments). I am not sure how to delete this thread, but it might be useful some day if someone is looking to do something similar.