SHARE
TWEET

ClusterMissile

a guest Oct 16th, 2012 36 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class ClusterMissileProjectile : MonoBehaviour {
  5.        
  6.         public float ExplodeDistance = 3;
  7.         public float MissileSpeed = 5;
  8.         public float ClusterAmount = 3;
  9.         public float ClusterSpreadDegrees = 120;
  10.        
  11.         public int ChildrenToSpawn = 3;
  12.        
  13.         private Vector3 _startPosition;
  14.        
  15.         // Use this for initialization
  16.         void Start () {
  17.                 _startPosition = transform.position;
  18.         }
  19.        
  20.         // Update is called once per frame
  21.         void Update () {
  22.                 if(Vector3.Distance(_startPosition, transform.position) >= ExplodeDistance){
  23.                         Explode();
  24.                 } else {
  25.                         transform.Translate(Vector3.up * MissileSpeed * Time.deltaTime);
  26.                 }
  27.         }
  28.        
  29.         /// <summary>
  30.         /// Explode this instance.
  31.         /// </summary>
  32.         void Explode(){
  33.                
  34.                 //this value will go down each time a new set of children is spawned
  35.                 if(ChildrenToSpawn > 0){
  36.                         var spreadMax = ClusterSpreadDegrees / 2;
  37.                         var spreadDistance = ClusterSpreadDegrees / (ClusterAmount-1);
  38.                        
  39.                         //initial rotation (in this case -22.5)
  40.                         var rotationAmountDegrees = -spreadMax;
  41.                         for (int i = 0; i < ClusterAmount; i++) {
  42.                                
  43.                                 var newMissile = Instantiate(this.gameObject, transform.position, transform.rotation) as GameObject;
  44.                                
  45.                                 //Vector3.right is towards the camera
  46.                                 newMissile.transform.RotateAround(transform.position, Vector3.right, rotationAmountDegrees);
  47.                                 rotationAmountDegrees += spreadDistance;
  48.                                
  49.                                 //make sure we don't create these infinitely
  50.                                 newMissile.GetComponent<ClusterMissileProjectile>().ChildrenToSpawn -= 1;
  51.                         }
  52.                        
  53.                 }
  54.                
  55.                 Destroy(gameObject);
  56.                
  57.         }
  58. }
RAW Paste Data
Want to get better at C#?
Learn to code C# in 2017
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top