Advertisement
Abuthar

PlayerMotor

Jun 18th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.20 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerMotor : MonoBehaviour
  6. {
  7.     public static PlayerMotor self;
  8.  
  9.     Vector3 storedAngle;
  10.     public int currentPoint;
  11.     public TileIdentifier currentTile;
  12.     public TileIdentifier nextTile;
  13.     public float speed;
  14.  
  15.     bool displaySelection;
  16.     bool buttonsEnabled;
  17.  
  18.     public TileIdentifier targetMissing;
  19.  
  20.     public AudioSource gemSound;
  21.  
  22.     public int drillTier;
  23.  
  24.     private void Awake()
  25.     {
  26.         self = this;
  27.     }
  28.  
  29.     void Update()
  30.     {
  31.         if (!TileManager.self.lossScreen.gameObject.activeInHierarchy)
  32.         {
  33.             if (transform.position == currentTile.points[currentTile.points.Count - 1] && nextTile != null)
  34.             {
  35.                 currentTile = nextTile;
  36.                 nextTile = null;
  37.                 currentPoint = 0;
  38.  
  39.                 Vector3 rot = transform.eulerAngles;
  40.                 transform.LookAt(currentTile.points[currentPoint]);
  41.                 transform.rotation = Quaternion.Euler(transform.eulerAngles.x, transform.eulerAngles.y, transform.eulerAngles.z);
  42.                 storedAngle = transform.eulerAngles;
  43.                 transform.rotation = Quaternion.Euler(rot);
  44.             }
  45.  
  46.             if (transform.position == currentTile.points[currentPoint] && currentPoint != currentTile.points.Count - 1)
  47.             {
  48.                 currentPoint += 1;
  49.  
  50.                 Vector3 rot = transform.eulerAngles;
  51.                 transform.LookAt(currentTile.points[currentPoint]);
  52.                 transform.rotation = Quaternion.Euler(transform.eulerAngles.x, transform.eulerAngles.y, transform.eulerAngles.z);
  53.                 storedAngle = transform.eulerAngles;
  54.                 transform.rotation = Quaternion.Euler(rot);
  55.             }
  56.  
  57.             if (transform.position != currentTile.points[currentTile.points.Count - 1])
  58.             {
  59.                 transform.position = Vector3.MoveTowards(transform.position, currentTile.points[currentPoint], speed / 25);
  60.                 transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(storedAngle), .075f * (1 + speed));
  61.             }
  62.         }
  63.     }
  64.  
  65.     private void OnTriggerEnter(Collider other)
  66.     {
  67.         if (other.gameObject.tag == "Track")
  68.         {
  69.             //Debug.Log("Entering Track!");
  70.             nextTile = other.GetComponent<TileIdentifier>();
  71.  
  72.             if (!nextTile.isMissing)
  73.             {
  74.                 if (!TileManager.self.buttonsEnabled)
  75.                 {
  76.                     RaycastHit[] hits;
  77.                     hits = Physics.RaycastAll(transform.position, transform.forward, 10);
  78.                     if (hits.Length == 0)
  79.                     {
  80.                         return;
  81.                     }
  82.                     else
  83.                     {
  84.                         for (int i = 0; i < hits.Length - 1; i++)
  85.                         {
  86.                             if (hits[i].collider.gameObject.GetComponent<TileIdentifier>() && !displaySelection)
  87.                             {
  88.                                 if (hits[i].collider.gameObject.GetComponent<TileIdentifier>().isMissing)
  89.                                 {
  90.                                     targetMissing = hits[i].collider.gameObject.GetComponent<TileIdentifier>();
  91.                                     displaySelection = true;
  92.                                     if (hits[i].point.x == targetMissing.GetComponent<Collider>().bounds.min.x)
  93.                                     {
  94.                                         targetMissing.direction = 1;
  95.                                     }
  96.                                     else if (hits[i].point.x == targetMissing.GetComponent<Collider>().bounds.max.x)
  97.                                     {
  98.                                         targetMissing.direction = 3;
  99.                                     }
  100.                                     else if (hits[i].point.x == targetMissing.GetComponent<Collider>().bounds.min.z)
  101.                                     {
  102.                                         targetMissing.direction = 0;
  103.                                     }
  104.                                     else if (hits[i].point.x == targetMissing.GetComponent<Collider>().bounds.max.z)
  105.                                     {
  106.                                         targetMissing.direction = 2;
  107.                                     }
  108.  
  109.                                     if (displaySelection == true)
  110.                                     {
  111.                                         displaySelection = false;
  112.                                         buttonsEnabled = true;
  113.                                         TileManager.self.ShowTiles();
  114.                                     }
  115.  
  116.                                     break;
  117.                                 }
  118.                             }
  119.                         }
  120.                     }
  121.  
  122.                     if (displaySelection == true)
  123.                     {
  124.                         displaySelection = false;
  125.                         buttonsEnabled = true;
  126.                         TileManager.self.ShowTiles();
  127.                     }
  128.                 }
  129.             }
  130.             else
  131.             {
  132.                 TileManager.self.lossScreen.gameObject.SetActive(true);
  133.             }
  134.  
  135.  
  136.             if (other.gameObject.tag == "GreenGem")
  137.             {
  138.                 TrackManager.t.gemScore += 1;
  139.                 TrackManager.t.Gems.text = ": " + TrackManager.t.gemScore;
  140.                 speed += .004f;
  141.                 other.gameObject.SetActive(false);
  142.                 other.GetComponent<GemIdenfidier>().InvokeReEnable();
  143.                 gemSound.Play();
  144.             }
  145.             if (other.gameObject.tag == "Key")
  146.             {
  147.                 TrackManager.t.AddKey();
  148.                 Destroy(other.gameObject);
  149.             }
  150.  
  151.             if (other.gameObject.tag == "Blockade")
  152.             {
  153.                 TileManager.self.lossScreen.gameObject.SetActive(true);
  154.             }
  155.         }
  156.         //Debug.Log("Done processing Track!");
  157.  
  158.     }
  159.  
  160.     private void OnDrawGizmos()
  161.     {
  162.         Debug.DrawRay(transform.position, transform.forward, Color.cyan, 4);
  163.     }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement