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

Killing object with 3 shots

Discussion in 'Getting Started' started by KarBis, Nov 24, 2019.

  1. KarBis

    KarBis

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

    I want to make asteroid get destroyed when it's hit 3 times, currently it's being destroyed after one shot only, how could i implement it? Thank you so much in advance!


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DestroyByContact : MonoBehaviour
    6. {
    7.     public GameObject explosion;
    8.     public GameObject playerExplosion;
    9.     public int scoreValue;
    10.     private GameController gameController;
    11.  
    12.     void Start ()
    13.     {
    14.         GameObject gameControllerObject = GameObject.FindGameObjectWithTag ("GameController");
    15.         if (gameControllerObject != null)
    16.         {
    17.             gameController = gameControllerObject.GetComponent <GameController>();
    18.         }
    19.         if (gameController == null)
    20.         {
    21.             Debug.Log ("Cannot find 'GameController' script");
    22.         }
    23.     }
    24.  
    25.  
    26.     void OnTriggerEnter(Collider other) {
    27.         if (other.tag =="Boundary")
    28.         {
    29.  
    30.         return;
    31.     }
    32.      if (other.tag == "Asteroid")
    33.         {
    34.             return;
    35.         }
    36.         Instantiate(explosion, transform.position, transform.rotation);
    37.         if (other.tag == "Player")
    38.         {
    39.             Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
    40.         }
    41.     gameController.AddScore (scoreValue);
    42.     Destroy(other.gameObject);
    43.     Destroy(gameObject);
    44.     }
    45. }
    46.  
     
  2. Blackuma

    Blackuma

    Joined:
    Jan 20, 2016
    Posts:
    1
    I would attach an script to your asteroid(s) which gives them a health pool set to a variable and a public class that removes one health each time the class is called. Have this called by your DestroyByContact class on trigger and have it destroy the Asteroid when the asteroids health is equal to 0.