Advertisement
Schicklerdev

Beacon.cs

Jan 16th, 2022
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.96 KB | None | 0 0
  1. using System.Collections;
  2. using UnityEngine;
  3.  
  4. public class Beacon : MonoBehaviour
  5. {
  6.  
  7.     AI ai;
  8.     Rigidbody2D myRigidbody2D;
  9.     [SerializeField] float lifetime = 0.25f;
  10.  
  11.     // Start is called before the first frame update
  12.     void Start()
  13.     {
  14.         myRigidbody2D = GetComponent<Rigidbody2D>();
  15.         ai = FindObjectOfType<AI>();
  16.         StartCoroutine(Kill());
  17.     }
  18.  
  19.     private void Update()
  20.     {
  21.         if (transform.position.x >= 4.3)
  22.         {
  23.             ai.beaconLocation = transform.position;
  24.             Destroy(gameObject);
  25.         }
  26.     }
  27.  
  28.  
  29.     IEnumerator Kill()
  30.     {
  31.         yield return new WaitForSeconds(lifetime);
  32.         SendLocation();
  33.     }
  34.  
  35.     public void SendLocation()
  36.     {
  37.         if (myRigidbody2D.velocity.x >= 0)
  38.         {
  39.             ai.beaconLocation = transform.position;
  40.             Destroy(gameObject);
  41.         }
  42.         else
  43.         {
  44.             Destroy(gameObject);
  45.         }
  46.     }
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement