Advertisement
tmoneygames

LaserTracking

Nov 22nd, 2019 (edited)
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.56 KB | Source Code | 0 0
  1. /*
  2.   Copyright 2019 John Wheeler<tmoneygames2019@gmail.com>
  3.   From Class: 2D Galaxy Space Shooter - gamedevhq.com
  4. */
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using UnityEngine;
  8.  
  9. public class LaserTracking : MonoBehaviour
  10. {
  11.  
  12.     // Variables
  13.     private Transform[] _enemies;
  14.     [SerializeField]
  15.     private Transform _enemy;
  16.     [SerializeField]
  17.     private bool _isEnemyAquired = false;
  18.     [SerializeField]
  19.     private bool _isBeingTargeted = false;
  20.     [SerializeField]
  21.     private float _speed = 5.0f;
  22.     [SerializeField]
  23.     private float _angleChangeSpeed = 15f;
  24.     private Rigidbody2D _rigidbody;
  25.     private AudioSource _audioSource;
  26.  
  27.     // Start is called before the first frame update
  28.     void Start()
  29.     {
  30.  
  31.         // checks to make sure that the enemy is initialized correctly or gives error message
  32.         if(_enemy == null)
  33.         {
  34.             Debug.LogError("The Enemy is NULL!");
  35.         }
  36.  
  37.         // cache the rigidbody component on the LaserTracking missle
  38.         _rigidbody = GetComponent<Rigidbody2D>();
  39.  
  40.         // checks to make sure that the rigidbody initialized correctly or gives error message
  41.         if(_rigidbody == null)
  42.         {
  43.             Debug.LogError("The Rigidbody2D is NULL!");
  44.         }
  45.  
  46.         // cache the _audioSource component on the LaserTracking Missle
  47.         _audioSource = GetComponent<AudioSource>();
  48.  
  49.         // checks to make sure that the AudioSource is initialized correctly or gives error message
  50.         if(_audioSource == null)
  51.         {
  52.             Debug.LogError("The AudioSource of the Tracking Laser is NULL!");
  53.         }
  54.     }
  55.  
  56.     // Update is called once per frame
  57.     void Update()
  58.     {
  59.         // sets the travel and speed of the rigidbody component on the tracking laser
  60.         _rigidbody.velocity = transform.up * _speed * Time.deltaTime;
  61.     }
  62.  
  63.     // a method that will find and track enemies with the tracking laser
  64.     public void FindAndTrackEnemies(Transform[] enemies)
  65.     {
  66.         // checks to see if any targets were found
  67.         if(_isEnemyAquired == false)
  68.         {
  69.  
  70.             // checks to make sure the _enemy is not null
  71.             if(_enemy != null)
  72.             {
  73.                 // if _enemy is not equal to null it turns the _isEnemyAquired to true
  74.                 _isEnemyAquired = true;
  75.             }
  76.             // else the enemy has to be tracked
  77.             else
  78.             {
  79.                 // if _enemy is not null
  80.                 if(_enemy != null)
  81.                 {
  82.                     // starts the targeting of the _enemy by the tracking laser
  83.                     Vector3 direction = (_enemy.position - transform.position);
  84.  
  85.                     // sets the direction to normal
  86.                     direction.Normalize();
  87.  
  88.                     // sets the initial angle direction variable
  89.                     float angle = Vector3.Angle(Vector3.up, direction);
  90.  
  91.                     // sets the cross direction variable
  92.                     float cross = Vector3.Cross(transform.up, direction).z;
  93.  
  94.                     // sets the _rigidbody tracking velocity for the tracking laser
  95.                     _rigidbody.angularVelocity = angle * cross * _angleChangeSpeed;
  96.                 }
  97.                 // else the enemy isn't being tracked
  98.                 else
  99.                 {
  100.                     // the _enemy is not being tracked
  101.                     _isEnemyAquired = false;
  102.                 }
  103.             }
  104.         }
  105.     }
  106.  
  107.     // a method that will get the closest spawning enemies to track and destroy
  108.     public void FindClosestTarget(Transform enemy, Transform[] _enemies)
  109.     {
  110.         // a local variable that sets the minimum distance from the player
  111.         float minDist = Mathf.Infinity;
  112.  
  113.         // a local transform for the closest enemy
  114.         Transform closestEnemy = null;
  115.  
  116.         // sets the current position to spawn
  117.         Vector3 posToSpawn = transform.position;
  118.  
  119.         // a foreach loop that will go through and find enemies to track
  120.         foreach( var _enemy in _enemies)
  121.         {
  122.  
  123.             // checks if the enemy from the enemycontainer is being targeted
  124.             if (_enemy.gameObject.name != "EnemyContainer" && enemy.gameObject.GetComponent<Enemy>()._isBeingTargeted == false)
  125.             {
  126.                 // sets a local float variable for the enemy distance to spawn
  127.                 float distance = Vector3.Distance(enemy.transform.position, posToSpawn);
  128.  
  129.                 //  checks if the distance is less than the minimum distance
  130.                 if(distance < minDist)
  131.                 {
  132.                     // sets the closest enemy to the enemy transform
  133.                     closestEnemy = enemy.transform;
  134.  
  135.                     // sets the minimum distance variable to the distance variable
  136.                     minDist = distance;
  137.                 }
  138.             }
  139.         }
  140.  
  141.         // a check to make sure that the closest enemy is not null
  142.         if(closestEnemy != null)
  143.         {
  144.             closestEnemy.transform.GetComponent<Enemy>()._isBeingTargeted = true;
  145.         }
  146.     }
  147.  
  148.     // a method that checks for collision with the enemy and then destroys itself
  149.     public void OnTriggerEnter2D(Collider2D other)
  150.     {
  151.         // checks the tag of the other to see if it's the enemy
  152.         if(other.tag == "Enemy")
  153.         {
  154.             // destroys the audio source component as soon as collison occurs
  155.             Destroy(_audioSource.gameObject);
  156.  
  157.             // destroys the laser tracking missle
  158.             Destroy(this.gameObject);
  159.         }
  160.     }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement