LegitTeddyBears

SpawnBomb

May 25th, 2017
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.97 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4.  
  5.  
  6. [RequireComponent(typeof(AudioSource))]
  7. public class SpawnBomb : MonoBehaviour
  8. {
  9.  
  10.     //Drag in the Bomb into the Component Inspector.
  11.     public GameObject Bombs;
  12.    
  13.     //Enter the Speed of the Bomb from the Component Inspector.
  14.     public float Bomb_Forward_Force;
  15.     //Max ammo until you need to reload
  16.     public int MaxAmmo;
  17.     //Current ammo
  18.     protected int Ammo;
  19.     Rigidbody rb;
  20.     //AudioSource
  21.     AudioSource Sounds;
  22.     public double m_ReArmTimer = 2;
  23.     // Use this for initialization
  24.     void Start()
  25.     {
  26.         //Get the rigidbody of the ship
  27.         rb = GetComponentInParent<Rigidbody>();
  28.         Sounds = GetComponent<AudioSource>();
  29.         //Set the current ammo to be equal to the max ammo.
  30.         Ammo = MaxAmmo;
  31.     }
  32. .
  33.     // Update is called once per frame
  34.     void Update()
  35.     {
  36.         //Start a timer to see how long it has been since we fired the bullet
  37.         m_ReArmTimer += Time.deltaTime;
  38.  
  39.         if (Input.GetMouseButtonDown(1))
  40.         {
  41.             if (Ammo >= 1)
  42.             {
  43.                 //Reset the timer of when we last shot
  44.                 m_ReArmTimer = 0;
  45.                 //The Bullet instantiation happens here.
  46.                 GameObject Temporary_Bullet_Handler;
  47.                 Temporary_Bullet_Handler = Instantiate(Bombs, transform.position, transform.rotation) as GameObject;
  48.                 Ammo = Ammo - 1;
  49.                 //Sometimes bullets may appear rotated incorrectly due to the way its pivot was set from the original modeling package.
  50.                 //This is EASILY corrected here, you might have to rotate it from a different axis and or angle based on your particular mesh.
  51.                 //Temporary_Bullet_Handler.transform.Rotate(Vector3.down * 90);
  52.  
  53.                 //Retrieve the Rigidbody component from the instantiated Bullet and control it.
  54.                 Rigidbody Temporary_RigidBody;
  55.                 Temporary_RigidBody = Temporary_Bullet_Handler.GetComponent<Rigidbody>();
  56.  
  57.                 //Set the velocity of the bomb to the same as the ship, this effectively prevents you from running into your own bombs
  58.                 Temporary_RigidBody.velocity = rb.velocity;
  59.  
  60.                 //Tell the bullet to be "pushed" forward by an amount set by Bullet_Forward_Force.
  61.                 Temporary_RigidBody.AddForce(transform.forward * Bomb_Forward_Force);
  62.  
  63.                 //Basic Clean Up, set the Bullets to self destruct after 10 Seconds, I am being generous here, normally 3 seconds is plenty.
  64.                 Destroy(Temporary_Bullet_Handler, 10.0f);
  65.                 //Play our fire sound
  66.                 Sounds.Play();
  67.             }
  68.         }
  69.         //If you have no ammo reload!
  70.         if (Ammo <= 0)
  71.         {  
  72.             //Start out couroutine
  73.             StartCoroutine(Reload());
  74.         }
  75.     }
  76.     // This is the reload function that we call when out of ammo
  77.     private IEnumerator Reload()
  78.     {
  79.         yield return new WaitForSeconds(3);
  80.         Ammo = MaxAmmo;
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment