Advertisement
Guest User

TrackingSystem

a guest
Sep 20th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.70 KB | None | 0 0
  1. using System.Collections;
  2. using UnityEngine;
  3.  
  4. [RequireComponent(typeof(ParticleSystem))]
  5. [RequireComponent(typeof(LineRenderer))]
  6. public class TrackingSystem : MonoBehaviour {
  7.  
  8.     GameObject target;
  9.     GameObject cube;
  10.     LineRenderer gunLine;
  11.     ParticleSystem gunParticles;
  12.     Light gunLight;
  13.     LayerMask playerMask;
  14.     LayerMask cubeMask;
  15.     Vector3 startPosition;
  16.     Ray shootRay;
  17.     RaycastHit hit;
  18.     int shotCounter = 50;
  19.     float shotDuration = 3f;
  20.     float timeBetweenShots = 50f;
  21.     float range = 20f;
  22.     float angleRange = 40;
  23.     float firstAngle;
  24.     float maxAngle;
  25.     float angle;
  26.     bool foundTarget;
  27.     [SerializeField]
  28.     bool setHanging;
  29.     [SerializeField]
  30.     bool setPatrolling;
  31.     bool isPatrolling;
  32.     bool isShooting;
  33.     bool wasShooting;
  34.  
  35.     void Awake() {
  36.  
  37.         gunParticles = GetComponent<ParticleSystem>();
  38.         gunLight = GetComponentInChildren<Light>();
  39.         gunLine = GetComponent<LineRenderer>();
  40.  
  41.         target = GameObject.FindGameObjectWithTag("Player");
  42.         playerMask = LayerMask.GetMask("Player");
  43.         cubeMask = LayerMask.GetMask("Cube");
  44.     }
  45.  
  46.     void Start() {
  47.  
  48.         startPosition = transform.position;
  49.         firstAngle = transform.eulerAngles.y;
  50.         maxAngle = firstAngle + angleRange;
  51.  
  52.         gunLine.startColor = Color.red;
  53.         gunLine.endColor = Color.red;
  54.         gunLine.SetPosition(0, transform.position);
  55.  
  56.         if (setPatrolling)
  57.         {
  58.             isPatrolling = true;
  59.         }
  60.     }
  61.  
  62.     void Update() {
  63.  
  64.         CheckForRaycast();
  65.         CheckForShooting();
  66.         CountForShooting();
  67.  
  68.         if (!isShooting)
  69.         {
  70.             DrawLaser();
  71.         }
  72.         if (!isPatrolling && foundTarget)
  73.         {
  74.             StartCoroutine(RotateToTarget());
  75.         }
  76.         if (!foundTarget && isPatrolling)
  77.         {
  78.             StartCoroutine(Patrol());
  79.         }
  80.     }
  81.  
  82.     void CountForShooting() {
  83.  
  84.         if (isShooting && (shotCounter == shotDuration))
  85.         {
  86.             DisableEffects();
  87.         }
  88.         else if (isShooting && shotCounter == timeBetweenShots)
  89.         {
  90.             Shoot();
  91.         }
  92.         else if (shotCounter > timeBetweenShots)
  93.         {
  94.             shotCounter = 0;
  95.         }
  96.  
  97.         shotCounter++;
  98.     }
  99.  
  100.     void CheckForRaycast() {
  101.  
  102.         if (setHanging)
  103.         {
  104.             if (Physics.Raycast(transform.position, transform.position + transform.forward * range - target.transform.position, out hit, range, playerMask))
  105.             {
  106.                 isShooting = true;
  107.                 foundTarget = true;
  108.             }
  109.         }
  110.         else
  111.         {
  112.             if (Physics.Raycast(transform.position, transform.position + transform.forward * range - target.transform.position + new Vector3(0, 1), out hit, range, playerMask))
  113.             {
  114.                 isShooting = true;
  115.                 foundTarget = true;
  116.             }
  117.         }
  118.     }
  119.  
  120.     void CheckForShooting() {
  121.  
  122.         if (isShooting && (maxAngle >= transform.eulerAngles.y || firstAngle - angleRange <= transform.eulerAngles.y))
  123.         {
  124.             if (setPatrolling)
  125.             {
  126.                 isPatrolling = false;
  127.             }
  128.            
  129.             StopAllCoroutines();
  130.             StartCoroutine(RotateToTarget());
  131.         }
  132.         if (!isShooting && wasShooting)
  133.         {
  134.             if (setPatrolling)
  135.             {
  136.                 isPatrolling = true;
  137.             }
  138.             StopAllCoroutines();
  139.             StartCoroutine(BackToFirstPosition());
  140.         }
  141.     }
  142.  
  143.     public void DisableEffects() {
  144.  
  145.         gunLine.enabled = false;
  146.         gunLight.enabled = false;
  147.     }
  148.  
  149.     void DrawLaser() {
  150.  
  151.         gunLine.enabled = true;
  152.  
  153.         if (Physics.Raycast(transform.position, transform.forward, out hit, range, cubeMask))
  154.         {
  155.             gunLine.SetPosition(1, hit.point);
  156.         }
  157.         else
  158.         {
  159.             if (setHanging && !setPatrolling)
  160.             {
  161.                 gunLine.SetPosition(1, transform.position + transform.forward * range + new Vector3(0,-4f));
  162.             }
  163.             else
  164.             {
  165.                 gunLine.SetPosition(1, transform.position + transform.forward * range);
  166.             }
  167.         }
  168.     }
  169.  
  170.     void Shoot() {
  171.  
  172.         wasShooting = false;
  173.         shotCounter = 0;
  174.  
  175.         gunParticles.Stop();
  176.         gunParticles.Play();
  177.  
  178.         gunLight.enabled = true;
  179.         gunLine.enabled = true;
  180.  
  181.         if (Physics.Raycast(shootRay, out hit, range))
  182.         {
  183.             gunLine.SetPosition(1, hit.point);
  184.         }
  185.         else
  186.         {
  187.             gunLine.SetPosition(1, transform.position + transform.forward * range);
  188.         }
  189.     }
  190.  
  191.     public IEnumerator RotateToTarget() {
  192.  
  193.         for (float t = 0.0f; t < 1.0f;)
  194.         {
  195.             t += Time.deltaTime * 0.8f;
  196.  
  197.             Quaternion rotate = Quaternion.LookRotation(target.transform.position - transform.position);
  198.             transform.rotation = Quaternion.Slerp(transform.rotation, rotate, t);
  199.  
  200.             if (firstAngle + angleRange <= transform.eulerAngles.y || firstAngle - angleRange >= transform.eulerAngles.y)
  201.             {
  202.                isShooting = false;
  203.                wasShooting = true;
  204.                foundTarget = false;
  205.             }
  206.  
  207.             yield return null;
  208.         }
  209.     }
  210.  
  211.     public IEnumerator Patrol() {
  212.  
  213.         for (float t = 0.0f; t < 1.0f;)
  214.         {
  215.             t += Time.deltaTime * 0.1f;
  216.             angle = Mathf.LerpAngle(firstAngle, maxAngle, t);
  217.  
  218.             if (setHanging)
  219.             {
  220.                 transform.eulerAngles = new Vector3(10, angle, 0);
  221.             }
  222.             else
  223.             {
  224.                 transform.eulerAngles = new Vector3(0, angle, 0);
  225.             }
  226.  
  227.             yield return null;
  228.         }
  229.         yield return StartCoroutine(GoBack());
  230.     }
  231.  
  232.     public IEnumerator GoBack() {
  233.  
  234.         for (float t = 0.0f; t < 1.0f;)
  235.         {
  236.             t += Time.deltaTime * 0.1f;
  237.             angle = Mathf.LerpAngle(maxAngle, firstAngle - angleRange, t);
  238.  
  239.             if (setHanging)
  240.             {
  241.                 transform.eulerAngles = new Vector3(10, angle, 0);
  242.             }
  243.             else
  244.             {
  245.                 transform.eulerAngles = new Vector3(0, angle, 0);
  246.             }
  247.  
  248.             yield return null;
  249.         }
  250.         yield return StartCoroutine(GoAgain());
  251.     }
  252.  
  253.     public IEnumerator GoAgain() {
  254.  
  255.         for (float t = 0.0f; t < 1.0f;)
  256.         {
  257.             t += Time.deltaTime * 0.1f;
  258.             angle = Mathf.LerpAngle(firstAngle - angleRange, maxAngle, t);
  259.  
  260.             if (setHanging)
  261.             {
  262.                 transform.eulerAngles = new Vector3(10, angle, 0);
  263.             }
  264.             else
  265.             {
  266.                 transform.eulerAngles = new Vector3(0, angle, 0);
  267.             }
  268.  
  269.             yield return null;
  270.         }
  271.         yield return StartCoroutine(GoBack());
  272.     }
  273.  
  274.     public IEnumerator BackToFirstPosition() {
  275.  
  276.         for (float t = 0.0f; t < 1.0f;)
  277.         {
  278.             t += Time.deltaTime * 0.1f;
  279.             angle = Mathf.LerpAngle(transform.eulerAngles.y, firstAngle, t);
  280.  
  281.             if (setHanging)
  282.             {
  283.                 transform.eulerAngles = new Vector3(10, angle, 0);
  284.             }
  285.             else
  286.             {
  287.                 transform.eulerAngles = new Vector3(0, angle, 0);
  288.             }
  289.  
  290.             foundTarget = false;
  291.  
  292.             if (setPatrolling)
  293.             {
  294.                 isPatrolling = true;
  295.             }
  296.  
  297.             yield return null;
  298.         }
  299.  
  300.         if (setPatrolling)
  301.         {
  302.             yield return StartCoroutine(Patrol());
  303.         }
  304.     }
  305. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement