Advertisement
Guest User

OrbControl.cs

a guest
Dec 28th, 2016
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.11 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System;
  5.  
  6. public class OrbControl : MonoBehaviour
  7. {
  8.     int currentOrb = 0;
  9.     int maxOrbs = 3;
  10.  
  11.     public Transform[] spawnPos;
  12.  
  13.     public GameObject airOrb;
  14.     public GameObject fireOrb;
  15.     public GameObject waterOrb;
  16.    
  17.     public bool allowOrbit = false; // if set to true then orbiting functionality needs to be added
  18.     public bool overRideCurrentOrb = false; // if set to true then "currentOrb" will not be used
  19.  
  20.     public List<GameObject> orbs = new List<GameObject>();
  21.  
  22.     void Start()
  23.     {
  24.         if (maxOrbs < spawnPos.Length)
  25.         {
  26.             maxOrbs = spawnPos.Length;
  27.         }
  28.     }
  29.    
  30.     void Update()
  31.     {
  32.         if (Input.GetKeyDown ("q") && currentOrb < maxOrbs)
  33.         {
  34.             spawnAirOrb ();
  35.         }
  36.  
  37.         if (Input.GetKeyDown ("w") && currentOrb < maxOrbs)
  38.         {
  39.             spawnFireOrb ();
  40.         }
  41.  
  42.         if (Input.GetKeyDown ("e") && currentOrb < maxOrbs)
  43.         {
  44.             spawnWaterOrb ();
  45.         }
  46.     }
  47.  
  48.     void spawnAirOrb()
  49.     {
  50.         spawnOrb(airOrb, 0);
  51.     }
  52.  
  53.     void spawnFireOrb()
  54.     {
  55.         spawnOrb(fireOrb, 1);
  56.     }
  57.  
  58.     void spawnWaterOrb()
  59.     {
  60.         spawnOrb(waterOrb, 2);
  61.     }
  62.  
  63.     void spawnOrb(GameObject orb, int index)
  64.     {
  65.         if (orb != null)
  66.         {
  67.             if (!overRideCurrentOrb)
  68.             {
  69.                 index = currentOrb;
  70.             }
  71.  
  72.             GameObject oldOrb = GameObject.Find(orb.name + "(Clone)");
  73.             Vector3 position = spawnPos [index].position;
  74.             Quaternion rotation = spawnPos [index].rotation;
  75.            
  76.             if (orbs.Count == maxOrbs)
  77.             {
  78.                 for (int i = 0; i < orbs.Count; i++)
  79.                 {
  80.                     if (((!allowOrbit) && (orbs[i].transform.position == position)) ||
  81.                         ((allowOrbit) && (orbs[i] != null)))
  82.                     {
  83.                         oldOrb = orbs[i];
  84.                         orbs.RemoveAt(i);
  85.                         position = oldOrb.transform.position;
  86.                         rotation = oldOrb.transform.rotation;
  87.                         break;
  88.                     }
  89.                 }
  90.             }
  91.            
  92.            
  93.             if (oldOrb != null)
  94.             {
  95.                 bool destroyOrb = (oldOrb.transform.position == position);
  96.                 if (allowOrbit)
  97.                 {
  98.                     // if orbiting is allowed then set the position and rotation values to the current object
  99.                     position = oldOrb.transform.position;
  100.                     rotation = oldOrb.transform.rotation;
  101.                 }
  102.                 if (destroyOrb)
  103.                 {
  104.                     Destroy(oldOrb);
  105.                 }
  106.             }
  107.             GameObject spawnedOrb = (GameObject)Instantiate (orb, position, rotation);
  108.             orbs.Add(spawnedOrb);
  109.  
  110.             currentOrb++;
  111.             if (currentOrb >= maxOrbs)
  112.             {
  113.                 currentOrb = 0;
  114.             }
  115.         }
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement