Advertisement
l3enjamin

CyberSiege VR - DirectMinions.cs

May 8th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.35 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class DirectMinions : MonoBehaviour {
  6.  
  7.     public SteamVR_TrackedObject Controller;
  8.     public SteamVR_Controller.Device Device;
  9.     public Vector3 DirectedTo;
  10.     public bool Directed, Controlled, RayHit, MessageSent, BobbleOut;
  11.     public SphereCollider Sphere;
  12.     public RaycastHit Hit;
  13.     public float Speed = 15f, IdleTimer, VibTimer, VibCD = 1f, BobbleTimer;
  14.     public GameObject[] Minions;
  15.     public Vector2 Touchpad;
  16.     public int Dir;
  17.     public GameObject FoundEnd, Switch, Play, ClickSFX, HoverSFX, DirectSFX, Hand, Reticle, BobbleSpawnVFX, Beam, HitObject;
  18.     public Material MaterialToBeAssigned, OriginalTowerMaterial, OriginalWindowMaterial;
  19.     public MeshRenderer CurrentTowerMesh, CurrentWindowMesh;
  20.     LayerMask DirectOnSurfaces = ((1 << 13) | (1 << 16) | (1 << 17) | (1 << 20) | (1 << 21));
  21.     LayerMask EnergyUpgrade = ((1 << 17));
  22.  
  23.     void Start () {
  24.         Controller = GetComponent<SteamVR_TrackedObject> ();
  25.         Switch = GameObject.FindGameObjectWithTag ("PlayArea");
  26.         Play = GameObject.FindGameObjectWithTag ("Player");
  27.     }
  28.  
  29.     void Update () {
  30.         Device = SteamVR_Controller.Input((int)Controller.index);
  31.         //Detach minions from energy
  32.         if (Device.GetPressDown (SteamVR_Controller.ButtonMask.Grip)) {
  33.             Device.TriggerHapticPulse (800);
  34.             Detach ();
  35.         }
  36.         //Pause function
  37.         if (Device.GetTouchDown (SteamVR_Controller.ButtonMask.ApplicationMenu)) {
  38.             Switch.transform.Find ("3DGUI").gameObject.GetComponent<GUIManager> ().OnPause ();
  39.         }
  40.         if (Hand == null) {
  41.             Hand = transform.Find ("Model").gameObject;
  42.         }
  43.         Minions = GameObject.FindGameObjectsWithTag ("Minion");
  44.         if (Hand != null) {
  45.             RaycastHit aim;
  46.             //Move the reticle according to where the controller is looking
  47.             if (Physics.Raycast (transform.position, transform.forward, out aim, Mathf.Infinity, DirectOnSurfaces)) {
  48.                 Reticle.transform.position = aim.point;
  49.             }
  50.             //Pressing the left trigger activates this whole function
  51.             if (Device.GetPressDown (SteamVR_Controller.ButtonMask.Trigger)) {
  52.                 //For the tutorial
  53.                 if (Switch.GetComponent<SwitchPath> ().bulletfired) {
  54.                     if (MessageSent == false) {
  55.                         Dir += 1;
  56.                         if (Dir >= 3) {
  57.                             Switch.GetComponent<SwitchPath> ().miniondirected = true;
  58.                             MessageSent = true;
  59.                         }
  60.                     }
  61.                 }
  62.                 //If the bobble is already out
  63.                 if (BobbleOut) {
  64.                     FoundEnd = GameObject.FindGameObjectWithTag ("EndCommand");
  65.                     Directed = false;
  66.                     if (FoundEnd != null) {
  67.                         if (HitObject != null) {
  68.                             //Changes the materials of what it hits to indicate selection
  69.                             if (HitObject.CompareTag ("TowerBobbleTarget")) {
  70.                                 CurrentTowerMesh.sharedMaterial = OriginalTowerMaterial;
  71.                                 CurrentWindowMesh.sharedMaterial = OriginalWindowMaterial;
  72.                             } else if (HitObject.CompareTag ("Gate") || HitObject.CompareTag ("FinalGate")) {
  73.                                 CurrentTowerMesh.sharedMaterial = OriginalTowerMaterial;
  74.                             }
  75.                         }
  76.                         Destroy (FoundEnd);
  77.                     }
  78.                     BobbleTimer = 0;
  79.                     BobbleOut = false;
  80.                 }
  81.                 //If the bobble is not out, spawn a bobble at the end of the raycast
  82.                 else if (!BobbleOut) {
  83.                     if (Physics.Raycast (transform.position, transform.forward, out Hit, Mathf.Infinity, DirectOnSurfaces)) {
  84.                         if (Hit.transform.CompareTag ("TowerBobbleTarget") || Hit.transform.CompareTag ("Upgrade") || Hit.transform.CompareTag ("Gate") || Hit.transform.CompareTag ("FinalGate")) {
  85.                             Minions = GameObject.FindGameObjectsWithTag ("Minion");
  86.                             foreach (GameObject min in Minions) {
  87.                                 min.GetComponent<Minion> ().Target = Hit.transform.gameObject;
  88.                             }
  89.                             if (Hit.transform.CompareTag ("TowerBobbleTarget") || Hit.transform.CompareTag ("Gate") || Hit.transform.CompareTag ("FinalGate")) {
  90.                                 HitObject = Hit.transform.gameObject;
  91.                                 if (Hit.transform.CompareTag ("TowerBobbleTarget")) {
  92.                                     CurrentTowerMesh = Hit.transform.parent.Find ("CyberTower-Base").GetComponent<MeshRenderer> ();
  93.                                     CurrentWindowMesh = Hit.transform.parent.Find ("CyberTower-Windows").GetComponent<MeshRenderer> ();
  94.                                 } else {
  95.                                     CurrentTowerMesh = Hit.transform.GetComponent<MeshRenderer> ();
  96.                                 }
  97.                                 OriginalTowerMaterial = CurrentTowerMesh.sharedMaterial;
  98.                                 if (Hit.transform.CompareTag ("TowerBobbleTarget")) {
  99.                                     OriginalWindowMaterial = CurrentWindowMesh.sharedMaterial;
  100.                                     CurrentWindowMesh.sharedMaterial = MaterialToBeAssigned;
  101.                                 }
  102.                                 CurrentTowerMesh.sharedMaterial = MaterialToBeAssigned;
  103.                             }
  104.                             //The minions access this variable to decide their target
  105.                             DirectedTo = Hit.transform.gameObject.transform.position;
  106.                         }
  107.                         //The minions access this variable to decide their target
  108.                         else {
  109.                             DirectedTo = Hit.point;
  110.                         }
  111.                         Directed = true;
  112.                         IdleTimer = 0f;
  113.                         Controlled = false;
  114.                         Device.TriggerHapticPulse (1000);
  115.                         Instantiate (Sphere, Hit.point, Quaternion.identity);
  116.                         Instantiate (BobbleSpawnVFX, Hit.point, Quaternion.identity);
  117.                         Instantiate (DirectSFX, transform.position, transform.rotation);
  118.                         BobbleOut = true;
  119.                     }
  120.                 }
  121.             }
  122.         }
  123.         //Bobble timer
  124.         if (BobbleOut) {
  125.             BobbleTimer += 1 * Time.deltaTime;
  126.             if (BobbleTimer > 19) {
  127.                 FoundEnd = GameObject.FindGameObjectWithTag ("EndCommand");
  128.                 Directed = false;
  129.                 if (FoundEnd != null) {
  130.                     if (HitObject != null) {
  131.                         if (HitObject.CompareTag ("TowerBobbleTarget") || HitObject.CompareTag ("Gate") || HitObject.CompareTag ("FinalGate")) {
  132.                             CurrentTowerMesh.sharedMaterial = OriginalTowerMaterial;
  133.                         }
  134.                         if (HitObject.CompareTag ("TowerBobbleTarget")) {
  135.                             CurrentWindowMesh.sharedMaterial = OriginalWindowMaterial;
  136.                         }
  137.                     }
  138.                     Destroy (FoundEnd);
  139.                 }
  140.                 BobbleTimer = 0;
  141.                 BobbleOut = false;
  142.             }
  143.         }
  144.         Touchpad = Device.GetAxis(EVRButtonId.k_EButton_SteamVR_Touchpad);
  145.         if (Device.GetPressDown (SteamVR_Controller.ButtonMask.Touchpad)) {
  146.             //Recalling the bobble if you touch the touchpad
  147.             FoundEnd = GameObject.FindGameObjectWithTag ("EndCommand");
  148.             Directed = false;
  149.             if (FoundEnd != null) {
  150.                 if (HitObject != null) {
  151.                     if (HitObject.CompareTag ("TowerBobbleTarget") || HitObject.CompareTag ("Gate") || HitObject.CompareTag ("FinalGate")) {
  152.                         CurrentTowerMesh.sharedMaterial = OriginalTowerMaterial;
  153.                     }
  154.                     if (HitObject.CompareTag ("TowerBobbleTarget")) {
  155.                         CurrentWindowMesh.sharedMaterial = OriginalWindowMaterial;
  156.                     }
  157.                 }
  158.             }
  159.         }
  160.         if (Device.GetPressUp (SteamVR_Controller.ButtonMask.Touchpad)) {
  161.             Controlled = false;
  162.         }
  163.         if (Device.GetPress(SteamVR_Controller.ButtonMask.Touchpad))
  164.         {
  165.             Minions = GameObject.FindGameObjectsWithTag ("Minion");
  166.             if (Touchpad.y > 0.2f) {
  167.                 //Move all minions forward from the player
  168.                 IdleTimer = 0f;
  169.                 Controlled = true;
  170.                 foreach (GameObject minion in Minions) {
  171.                     Vector3 Dir = Play.transform.forward;
  172.                     Vector3 movement = Dir.normalized * Speed;
  173.                     minion.GetComponent<CharacterController> ().SimpleMove (movement);
  174.                 }
  175.                 VibTimer += 1 * Time.deltaTime;
  176.                 if (VibTimer >= VibCD) {
  177.                     Device.TriggerHapticPulse (500);
  178.                     VibTimer = 0f;
  179.                 }
  180.             }
  181.             else if (Touchpad.y < -0.2f)
  182.             {
  183.                 //Move all minions backwards from the player
  184.                 IdleTimer = 0f;
  185.                 Controlled = true;
  186.                 foreach (GameObject minion in Minions) {
  187.                     Vector3 Dir = -Play.transform.forward;
  188.                     Vector3 movement = Dir.normalized * Speed;
  189.                     minion.GetComponent<CharacterController> ().SimpleMove (movement);
  190.                 }
  191.                 VibTimer += 1 * Time.deltaTime;
  192.                 if (VibTimer >= VibCD) {
  193.                     Device.TriggerHapticPulse (500);
  194.                     VibTimer = 0f;
  195.                 }
  196.             }
  197.             if (Touchpad.x > 0.3f)
  198.             {
  199.                 //Move all minions right of the player
  200.                 IdleTimer = 0f;
  201.                 Controlled = true;
  202.                 foreach (GameObject minion in Minions) {
  203.                     Vector3 Dir = Play.transform.right;
  204.                     Vector3 movement = Dir.normalized * Speed;
  205.                     minion.GetComponent<CharacterController> ().SimpleMove (movement);
  206.                 }
  207.                 VibTimer += 1 * Time.deltaTime;
  208.                 if (VibTimer >= VibCD) {
  209.                     Device.TriggerHapticPulse (500);
  210.                     VibTimer = 0f;
  211.                 }
  212.             }
  213.             else if (Touchpad.x < -0.3f)
  214.             {
  215.                 //Move all minions left of the player
  216.                 IdleTimer = 0f;
  217.                 Controlled = true;
  218.                 foreach (GameObject minion in Minions) {
  219.                     Vector3 Dir = -Play.transform.right;
  220.                     Vector3 movement = Dir.normalized * Speed;
  221.                     minion.GetComponent<CharacterController> ().SimpleMove (movement);
  222.                 }
  223.                 VibTimer += 1 * Time.deltaTime;
  224.                 if (VibTimer >= VibCD) {
  225.                     Device.TriggerHapticPulse (500);
  226.                     VibTimer = 0f;
  227.                 }
  228.             }
  229.         }
  230.         //If the minions aren't controlled for 2 seconds, they go idle
  231.         if (Controlled == false && Directed == false && Switch.GetComponent<SwitchPath>().miniondirected == true) {
  232.             IdleTimer += 1 * Time.deltaTime;
  233.             if (IdleTimer > 2f) {
  234.                 Controlled = false;
  235.             }
  236.         }
  237.     }
  238.  
  239.     //Detach function
  240.     void Detach() {
  241.         RaycastHit Hit;
  242.         if (Physics.Raycast (transform.position, transform.forward, out Hit, Mathf.Infinity, EnergyUpgrade)) {
  243.             if (Hit.transform.tag == "CarriedUpgrade") {
  244.                 if (Hit.transform.GetComponent<RockGroupMove> () != null) {
  245.                     Hit.transform.GetComponent<RockGroupMove> ().RayHit = true;
  246.                 }
  247.                 if (Hit.transform.GetComponent<TutRockGroupMove> () != null) {
  248.                     Hit.transform.GetComponent<TutRockGroupMove> ().RayHit = true;
  249.                 }
  250.             }
  251.         }
  252.     }
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement