Advertisement
nikcio

Untitled

Sep 14th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.84 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class TreeMain : MonoBehaviour {
  5.  
  6.     private GameObject tree;
  7.     private bool callOnce;
  8.  
  9.     public int treeHealth = 5;
  10.     public int speed = 2;
  11.    
  12.     void Start ()
  13.     {
  14.         callOnce = false;
  15.         tree = this.gameObject;
  16.     }
  17.    
  18.     void Update ()
  19.     {
  20.         if(treeHealth == 1)
  21.         {
  22.             callOnce = true;
  23.         }
  24.  
  25.         if((treeHealth <= 0) && (callOnce == true))
  26.         {
  27.             tree.AddComponent<Rigidbody>();
  28.             GetComponent<Rigidbody>().isKinematic = false;
  29.             GetComponent<Rigidbody>().AddForce(transform.forward * speed, ForceMode.Impulse);
  30.             DestroyTree();
  31.             callOnce = false;
  32.         }
  33.     }
  34.  
  35.     IEnumerator DestroyTree()
  36.     {
  37.         yield return new WaitForSeconds(7);
  38.         Destroy(tree);
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement