Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Copyright 2019 John Wheeler<tmoneygames2019@gmail.com>
- From Class: 2D Galaxy Space Shooter - gamedevhq.com
- */
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class LaserTracking : MonoBehaviour
- {
- // Variables
- private Transform[] _enemies;
- [SerializeField]
- private Transform _enemy;
- [SerializeField]
- private bool _isEnemyAquired = false;
- [SerializeField]
- private bool _isBeingTargeted = false;
- [SerializeField]
- private float _speed = 5.0f;
- [SerializeField]
- private float _angleChangeSpeed = 15f;
- private Rigidbody2D _rigidbody;
- private AudioSource _audioSource;
- // Start is called before the first frame update
- void Start()
- {
- // checks to make sure that the enemy is initialized correctly or gives error message
- if(_enemy == null)
- {
- Debug.LogError("The Enemy is NULL!");
- }
- // cache the rigidbody component on the LaserTracking missle
- _rigidbody = GetComponent<Rigidbody2D>();
- // checks to make sure that the rigidbody initialized correctly or gives error message
- if(_rigidbody == null)
- {
- Debug.LogError("The Rigidbody2D is NULL!");
- }
- // cache the _audioSource component on the LaserTracking Missle
- _audioSource = GetComponent<AudioSource>();
- // checks to make sure that the AudioSource is initialized correctly or gives error message
- if(_audioSource == null)
- {
- Debug.LogError("The AudioSource of the Tracking Laser is NULL!");
- }
- }
- // Update is called once per frame
- void Update()
- {
- // sets the travel and speed of the rigidbody component on the tracking laser
- _rigidbody.velocity = transform.up * _speed * Time.deltaTime;
- }
- // a method that will find and track enemies with the tracking laser
- public void FindAndTrackEnemies(Transform[] enemies)
- {
- // checks to see if any targets were found
- if(_isEnemyAquired == false)
- {
- // checks to make sure the _enemy is not null
- if(_enemy != null)
- {
- // if _enemy is not equal to null it turns the _isEnemyAquired to true
- _isEnemyAquired = true;
- }
- // else the enemy has to be tracked
- else
- {
- // if _enemy is not null
- if(_enemy != null)
- {
- // starts the targeting of the _enemy by the tracking laser
- Vector3 direction = (_enemy.position - transform.position);
- // sets the direction to normal
- direction.Normalize();
- // sets the initial angle direction variable
- float angle = Vector3.Angle(Vector3.up, direction);
- // sets the cross direction variable
- float cross = Vector3.Cross(transform.up, direction).z;
- // sets the _rigidbody tracking velocity for the tracking laser
- _rigidbody.angularVelocity = angle * cross * _angleChangeSpeed;
- }
- // else the enemy isn't being tracked
- else
- {
- // the _enemy is not being tracked
- _isEnemyAquired = false;
- }
- }
- }
- }
- // a method that will get the closest spawning enemies to track and destroy
- public void FindClosestTarget(Transform enemy, Transform[] _enemies)
- {
- // a local variable that sets the minimum distance from the player
- float minDist = Mathf.Infinity;
- // a local transform for the closest enemy
- Transform closestEnemy = null;
- // sets the current position to spawn
- Vector3 posToSpawn = transform.position;
- // a foreach loop that will go through and find enemies to track
- foreach( var _enemy in _enemies)
- {
- // checks if the enemy from the enemycontainer is being targeted
- if (_enemy.gameObject.name != "EnemyContainer" && enemy.gameObject.GetComponent<Enemy>()._isBeingTargeted == false)
- {
- // sets a local float variable for the enemy distance to spawn
- float distance = Vector3.Distance(enemy.transform.position, posToSpawn);
- // checks if the distance is less than the minimum distance
- if(distance < minDist)
- {
- // sets the closest enemy to the enemy transform
- closestEnemy = enemy.transform;
- // sets the minimum distance variable to the distance variable
- minDist = distance;
- }
- }
- }
- // a check to make sure that the closest enemy is not null
- if(closestEnemy != null)
- {
- closestEnemy.transform.GetComponent<Enemy>()._isBeingTargeted = true;
- }
- }
- // a method that checks for collision with the enemy and then destroys itself
- public void OnTriggerEnter2D(Collider2D other)
- {
- // checks the tag of the other to see if it's the enemy
- if(other.tag == "Enemy")
- {
- // destroys the audio source component as soon as collison occurs
- Destroy(_audioSource.gameObject);
- // destroys the laser tracking missle
- Destroy(this.gameObject);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement