Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class DirectMinions : MonoBehaviour {
- public SteamVR_TrackedObject Controller;
- public SteamVR_Controller.Device Device;
- public Vector3 DirectedTo;
- public bool Directed, Controlled, RayHit, MessageSent, BobbleOut;
- public SphereCollider Sphere;
- public RaycastHit Hit;
- public float Speed = 15f, IdleTimer, VibTimer, VibCD = 1f, BobbleTimer;
- public GameObject[] Minions;
- public Vector2 Touchpad;
- public int Dir;
- public GameObject FoundEnd, Switch, Play, ClickSFX, HoverSFX, DirectSFX, Hand, Reticle, BobbleSpawnVFX, Beam, HitObject;
- public Material MaterialToBeAssigned, OriginalTowerMaterial, OriginalWindowMaterial;
- public MeshRenderer CurrentTowerMesh, CurrentWindowMesh;
- LayerMask DirectOnSurfaces = ((1 << 13) | (1 << 16) | (1 << 17) | (1 << 20) | (1 << 21));
- LayerMask EnergyUpgrade = ((1 << 17));
- void Start () {
- Controller = GetComponent<SteamVR_TrackedObject> ();
- Switch = GameObject.FindGameObjectWithTag ("PlayArea");
- Play = GameObject.FindGameObjectWithTag ("Player");
- }
- void Update () {
- Device = SteamVR_Controller.Input((int)Controller.index);
- //Detach minions from energy
- if (Device.GetPressDown (SteamVR_Controller.ButtonMask.Grip)) {
- Device.TriggerHapticPulse (800);
- Detach ();
- }
- //Pause function
- if (Device.GetTouchDown (SteamVR_Controller.ButtonMask.ApplicationMenu)) {
- Switch.transform.Find ("3DGUI").gameObject.GetComponent<GUIManager> ().OnPause ();
- }
- if (Hand == null) {
- Hand = transform.Find ("Model").gameObject;
- }
- Minions = GameObject.FindGameObjectsWithTag ("Minion");
- if (Hand != null) {
- RaycastHit aim;
- //Move the reticle according to where the controller is looking
- if (Physics.Raycast (transform.position, transform.forward, out aim, Mathf.Infinity, DirectOnSurfaces)) {
- Reticle.transform.position = aim.point;
- }
- //Pressing the left trigger activates this whole function
- if (Device.GetPressDown (SteamVR_Controller.ButtonMask.Trigger)) {
- //For the tutorial
- if (Switch.GetComponent<SwitchPath> ().bulletfired) {
- if (MessageSent == false) {
- Dir += 1;
- if (Dir >= 3) {
- Switch.GetComponent<SwitchPath> ().miniondirected = true;
- MessageSent = true;
- }
- }
- }
- //If the bobble is already out
- if (BobbleOut) {
- FoundEnd = GameObject.FindGameObjectWithTag ("EndCommand");
- Directed = false;
- if (FoundEnd != null) {
- if (HitObject != null) {
- //Changes the materials of what it hits to indicate selection
- if (HitObject.CompareTag ("TowerBobbleTarget")) {
- CurrentTowerMesh.sharedMaterial = OriginalTowerMaterial;
- CurrentWindowMesh.sharedMaterial = OriginalWindowMaterial;
- } else if (HitObject.CompareTag ("Gate") || HitObject.CompareTag ("FinalGate")) {
- CurrentTowerMesh.sharedMaterial = OriginalTowerMaterial;
- }
- }
- Destroy (FoundEnd);
- }
- BobbleTimer = 0;
- BobbleOut = false;
- }
- //If the bobble is not out, spawn a bobble at the end of the raycast
- else if (!BobbleOut) {
- if (Physics.Raycast (transform.position, transform.forward, out Hit, Mathf.Infinity, DirectOnSurfaces)) {
- if (Hit.transform.CompareTag ("TowerBobbleTarget") || Hit.transform.CompareTag ("Upgrade") || Hit.transform.CompareTag ("Gate") || Hit.transform.CompareTag ("FinalGate")) {
- Minions = GameObject.FindGameObjectsWithTag ("Minion");
- foreach (GameObject min in Minions) {
- min.GetComponent<Minion> ().Target = Hit.transform.gameObject;
- }
- if (Hit.transform.CompareTag ("TowerBobbleTarget") || Hit.transform.CompareTag ("Gate") || Hit.transform.CompareTag ("FinalGate")) {
- HitObject = Hit.transform.gameObject;
- if (Hit.transform.CompareTag ("TowerBobbleTarget")) {
- CurrentTowerMesh = Hit.transform.parent.Find ("CyberTower-Base").GetComponent<MeshRenderer> ();
- CurrentWindowMesh = Hit.transform.parent.Find ("CyberTower-Windows").GetComponent<MeshRenderer> ();
- } else {
- CurrentTowerMesh = Hit.transform.GetComponent<MeshRenderer> ();
- }
- OriginalTowerMaterial = CurrentTowerMesh.sharedMaterial;
- if (Hit.transform.CompareTag ("TowerBobbleTarget")) {
- OriginalWindowMaterial = CurrentWindowMesh.sharedMaterial;
- CurrentWindowMesh.sharedMaterial = MaterialToBeAssigned;
- }
- CurrentTowerMesh.sharedMaterial = MaterialToBeAssigned;
- }
- //The minions access this variable to decide their target
- DirectedTo = Hit.transform.gameObject.transform.position;
- }
- //The minions access this variable to decide their target
- else {
- DirectedTo = Hit.point;
- }
- Directed = true;
- IdleTimer = 0f;
- Controlled = false;
- Device.TriggerHapticPulse (1000);
- Instantiate (Sphere, Hit.point, Quaternion.identity);
- Instantiate (BobbleSpawnVFX, Hit.point, Quaternion.identity);
- Instantiate (DirectSFX, transform.position, transform.rotation);
- BobbleOut = true;
- }
- }
- }
- }
- //Bobble timer
- if (BobbleOut) {
- BobbleTimer += 1 * Time.deltaTime;
- if (BobbleTimer > 19) {
- FoundEnd = GameObject.FindGameObjectWithTag ("EndCommand");
- Directed = false;
- if (FoundEnd != null) {
- if (HitObject != null) {
- if (HitObject.CompareTag ("TowerBobbleTarget") || HitObject.CompareTag ("Gate") || HitObject.CompareTag ("FinalGate")) {
- CurrentTowerMesh.sharedMaterial = OriginalTowerMaterial;
- }
- if (HitObject.CompareTag ("TowerBobbleTarget")) {
- CurrentWindowMesh.sharedMaterial = OriginalWindowMaterial;
- }
- }
- Destroy (FoundEnd);
- }
- BobbleTimer = 0;
- BobbleOut = false;
- }
- }
- Touchpad = Device.GetAxis(EVRButtonId.k_EButton_SteamVR_Touchpad);
- if (Device.GetPressDown (SteamVR_Controller.ButtonMask.Touchpad)) {
- //Recalling the bobble if you touch the touchpad
- FoundEnd = GameObject.FindGameObjectWithTag ("EndCommand");
- Directed = false;
- if (FoundEnd != null) {
- if (HitObject != null) {
- if (HitObject.CompareTag ("TowerBobbleTarget") || HitObject.CompareTag ("Gate") || HitObject.CompareTag ("FinalGate")) {
- CurrentTowerMesh.sharedMaterial = OriginalTowerMaterial;
- }
- if (HitObject.CompareTag ("TowerBobbleTarget")) {
- CurrentWindowMesh.sharedMaterial = OriginalWindowMaterial;
- }
- }
- }
- }
- if (Device.GetPressUp (SteamVR_Controller.ButtonMask.Touchpad)) {
- Controlled = false;
- }
- if (Device.GetPress(SteamVR_Controller.ButtonMask.Touchpad))
- {
- Minions = GameObject.FindGameObjectsWithTag ("Minion");
- if (Touchpad.y > 0.2f) {
- //Move all minions forward from the player
- IdleTimer = 0f;
- Controlled = true;
- foreach (GameObject minion in Minions) {
- Vector3 Dir = Play.transform.forward;
- Vector3 movement = Dir.normalized * Speed;
- minion.GetComponent<CharacterController> ().SimpleMove (movement);
- }
- VibTimer += 1 * Time.deltaTime;
- if (VibTimer >= VibCD) {
- Device.TriggerHapticPulse (500);
- VibTimer = 0f;
- }
- }
- else if (Touchpad.y < -0.2f)
- {
- //Move all minions backwards from the player
- IdleTimer = 0f;
- Controlled = true;
- foreach (GameObject minion in Minions) {
- Vector3 Dir = -Play.transform.forward;
- Vector3 movement = Dir.normalized * Speed;
- minion.GetComponent<CharacterController> ().SimpleMove (movement);
- }
- VibTimer += 1 * Time.deltaTime;
- if (VibTimer >= VibCD) {
- Device.TriggerHapticPulse (500);
- VibTimer = 0f;
- }
- }
- if (Touchpad.x > 0.3f)
- {
- //Move all minions right of the player
- IdleTimer = 0f;
- Controlled = true;
- foreach (GameObject minion in Minions) {
- Vector3 Dir = Play.transform.right;
- Vector3 movement = Dir.normalized * Speed;
- minion.GetComponent<CharacterController> ().SimpleMove (movement);
- }
- VibTimer += 1 * Time.deltaTime;
- if (VibTimer >= VibCD) {
- Device.TriggerHapticPulse (500);
- VibTimer = 0f;
- }
- }
- else if (Touchpad.x < -0.3f)
- {
- //Move all minions left of the player
- IdleTimer = 0f;
- Controlled = true;
- foreach (GameObject minion in Minions) {
- Vector3 Dir = -Play.transform.right;
- Vector3 movement = Dir.normalized * Speed;
- minion.GetComponent<CharacterController> ().SimpleMove (movement);
- }
- VibTimer += 1 * Time.deltaTime;
- if (VibTimer >= VibCD) {
- Device.TriggerHapticPulse (500);
- VibTimer = 0f;
- }
- }
- }
- //If the minions aren't controlled for 2 seconds, they go idle
- if (Controlled == false && Directed == false && Switch.GetComponent<SwitchPath>().miniondirected == true) {
- IdleTimer += 1 * Time.deltaTime;
- if (IdleTimer > 2f) {
- Controlled = false;
- }
- }
- }
- //Detach function
- void Detach() {
- RaycastHit Hit;
- if (Physics.Raycast (transform.position, transform.forward, out Hit, Mathf.Infinity, EnergyUpgrade)) {
- if (Hit.transform.tag == "CarriedUpgrade") {
- if (Hit.transform.GetComponent<RockGroupMove> () != null) {
- Hit.transform.GetComponent<RockGroupMove> ().RayHit = true;
- }
- if (Hit.transform.GetComponent<TutRockGroupMove> () != null) {
- Hit.transform.GetComponent<TutRockGroupMove> ().RayHit = true;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement