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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Destroy Gameobject when he's out of the Area

Discussion in '2D' started by Valenfar, Oct 21, 2015.

  1. Valenfar

    Valenfar

    Joined:
    Oct 7, 2015
    Posts:
    7
    Hi guys!
    Sorry for this question but, i'm already seeing a lot of tutorials and none as useful yet. So... I have my gaming area set in:

    xMin = -10
    xMax = 10
    yMin = -4.3
    yMax = 4.3

    And i have my collider code:

    Code (CSharp):
    1.     public GameObject explosion;        // Prefab of explosion effect.
    2.  
    3.  
    4.     void Start ()
    5.     {
    6.  
    7.     }
    8.  
    9.     void OnExplode()
    10.     {
    11.         // Create a quaternion with a random rotation in the z-axis.
    12.         Quaternion randomRotation = Quaternion.Euler(0f, 0f, Random.Range(0f, 360f));
    13.      
    14.         // Instantiate the explosion where the rocket is with the random rotation.
    15.         Instantiate(explosion, transform.position, randomRotation);
    16.     }
    17.  
    18.     void OnTriggerEnter2D (Collider2D col)
    19.     {
    20.         // If it hits an enemy...
    21.         if(col.tag == "Inimigo")
    22.         {
    23.             // ... find the Enemy script and call the Hurt function.
    24.             //col.gameObject.GetComponent<Inimigo>().Hurt();
    25.          
    26.             // Call the explosion instantiation.
    27.             OnExplode();
    28.          
    29.             // Destroy the rocket.
    30.             Destroy (gameObject);
    31.         }
    32.         // Otherwise if the player manages to shoot himself...
    33.         else if(col.gameObject.tag != "Player")
    34.         {
    35.             // Instantiate the explosion and destroy the rocket.
    36.             OnExplode();
    37.             Destroy (gameObject);
    38.         }
    39.  
    40.     }
    I want to know if there is a way to know when my object left the gaming area he can be destroyed. I try to pick up his position and compare with a vector3 but doesn't work

    This is the code where i try to compare the x Axis from a variable gArea (Gaming Area) and position of my object instance.

    Code (CSharp):
    1.     void Update ()
    2.     {
    3.         gArea = new Vector3 (Mathf.Clamp(rocket.position.x, -10f, 10f), Mathf.Clamp(rocket.position.y, -4.3f, 4.3f), 0.0f);
    4.         // If the fire button is pressed...
    5.         if(Input.GetButtonDown("Fire1"))
    6.         {
    7.  
    8.                 rocket.transform.localScale = new Vector3(xScale, yScale, 0.0F);
    9.                 // ... instantiate the rocket facing right and set it's velocity to the right.
    10.                 Rigidbody2D bulletInstance = Instantiate(rocket, transform.position, Quaternion.Euler(new Vector3(0,0,0))) as Rigidbody2D;
    11.                 bulletInstance.velocity = new Vector2(speed, 0);
    12.  
    13.             //Compare x position to destroy the object
    14.             if (bulletInstance.position.x == gArea.x)
    15.             {
    16.                 Destroy(rocket);
    17.             }
    18.         }
    19.  
    20.  
    21.     }

    Somebody can help me ?
     
  2. The-Little-Guy

    The-Little-Guy

    Joined:
    Aug 1, 2012
    Posts:
    297
    Assuming you are using a collider of sorts (BoxCollider2d, CircleCollider2d, PollygonCollider2d), you can use OnTriggerExit2D

    Code (CSharp):
    1. void OnTriggerExit2D(Collider2D other){
    2.     Destroy(rocket);
    3. }
     
    Valenfar likes this.
  3. VorpalSilence

    VorpalSilence

    Joined:
    Oct 22, 2015
    Posts:
    13
    In your code you are changing 'bulletInstance' every time you fire a bullet, you are then performing your check to see if it should be destroyed right inside your fire button input condition. So it will only check if it should destroy the object as you fire a new object, which replaces the first object since that part of your code is above the destroy check. It depends on exactly the effect you want, but I would suggest that you take your destroy check and place it in its own script called something like 'DestroyOutOfBounds' and attach that script to the prefab of your bullet. You will just then need to feed it the correct information in terms of your play area. Based on the info you posted my guess is something like this would work;

    Code (csharp):
    1.  
    2. float xMin = -10f;
    3. float xMax = 10f;
    4. float yMin = -4.3f;
    5. float yMax = 4.3f;
    6.  
    7. void Update()
    8. {
    9.     if (transform.position.x > xMax || transform.position.x < xMin)
    10.     {
    11.         Destroy(this);
    12.     }
    13. }
    14.  
     
    Valenfar likes this.
  4. Valenfar

    Valenfar

    Joined:
    Oct 7, 2015
    Posts:
    7
    Thx guys!

    I got it!:)