Search Unity

Avoiding two Game Objects Clashing each other

Discussion in 'Getting Started' started by KarBis, Sep 14, 2019.

  1. KarBis

    KarBis

    Joined:
    Sep 6, 2019
    Posts:
    9
    Hi guys,

    How can I avoid two game objects (flying asteroids) clashing with each other?

    I still want to be able to destroy them with shots, but not with one another.

    Thank you!
     
  2. Amon

    Amon

    Joined:
    Oct 18, 2009
    Posts:
    1,048
    Disable collision checking between asteroids or when they collide smash them into two smaller ones. Basically whatever you have in your collision method for asteroids, remove it so they don't get destroyed.

    Without seeing some code I'm limited in how I can help you.
     
  3. KarBis

    KarBis

    Joined:
    Sep 6, 2019
    Posts:
    9
    Here is the full script

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class DestroyByContact : MonoBehaviour
    7. {
    8.     public GameObject explosion;
    9.     public GameObject playerExplosion;
    10.     public int scoreValue;
    11.     private GameController gameController;
    12.  
    13.     void Start()
    14.     {
    15.         GameObject gameControllerObject = GameObject.FindWithTag("GameController");
    16.         if (gameControllerObject != null)
    17.         {
    18.             gameController = gameControllerObject.GetComponent<GameController>();
    19.         }
    20.         if (gameController == null)
    21.         {
    22.             Debug.Log("Cannot find 'GameController' script");
    23.         }
    24.     }
    25.  
    26.     void OnTriggerEnter(Collider other) {
    27.         if (other.tag =="Boundary")
    28.         {
    29.  
    30.         return;
    31.     }
    32.         Instantiate(explosion, transform.position, transform.rotation);
    33.         if (other.tag == "Player")
    34.         {
    35.             Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
    36.         }
    37.  
    38.         gameController.AddScore(scoreValue);
    39.         Destroy(other.gameObject);
    40.         Destroy(gameObject);
    41.     }
    42. }
    43.  
    Could you give a hint of commands to be used to exclude the colisions or as you mentioned to smash them into 2 smaller ones?
     
  4. Amon

    Amon

    Joined:
    Oct 18, 2009
    Posts:
    1,048
    Is that script attached to your asteroids? If it is then therein lies your problem.
     
  5. KarBis

    KarBis

    Joined:
    Sep 6, 2019
    Posts:
    9
    Yes, that's the one attached to the asteroids, could you give a hint what's the issue?
     
  6. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    14,540
    You're not checking if it's colliding with another asteroid. You're only checking for the boundary and the player before you issue the command to destroy it. Assuming you're tagging them as "Asteroid" change the code starting at line 27 to the following.

    Code (csharp):
    1. if (other.tag == "Boundary" || other.tag == "Asteroid")
    2. {
    3.     return;
    4. }
     
  7. KarBis

    KarBis

    Joined:
    Sep 6, 2019
    Posts:
    9
    I tried that, but didn't work
     
  8. KarBis

    KarBis

    Joined:
    Sep 6, 2019
    Posts:
    9
    but when I modified to the following, it worked, thanks so much for help!

    Code (CSharp):
    1.         if (other.tag =="Boundary")
    2.  
    3.         {
    4.                 return;
    5.            
    6.         }
    7.         if (other.tag == "Asteroid")
    8.  
    9.         {
    10.             return;
    11.         }