Advertisement
SizilStank

Untitled

Feb 26th, 2020
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.54 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class HomingPlayerLaser : MonoBehaviour
  6. {
  7.  
  8.     [SerializeField] private float _speed = 8f;
  9.     [SerializeField] private GameObject _bossHitExplosionAnimPrefab;
  10.  
  11.     [SerializeField] private Transform target; // enemyTransform
  12.  
  13.  
  14.     SpawnManager _spawnManager;
  15.  
  16.  
  17.     // Use this for initialization
  18.     void Start()
  19.     {
  20.         _spawnManager = GameObject.Find("SpawnManager").GetComponent<SpawnManager>();
  21.        
  22.  
  23.         if (target != null) // but it IS NULL at start
  24.         {
  25.             target = GameObject.FindGameObjectWithTag("Enemy").transform;
  26.             target = _spawnManager._enemyPrefab.transform;
  27.         }
  28.     }
  29.  
  30.     // Update is called once per frame
  31.     void Update()
  32.     {
  33.        
  34.         if (target == null)
  35.         {
  36.             transform.Translate(Vector3.up * _speed * Time.deltaTime);
  37.         }
  38.            
  39.        
  40.  
  41.        
  42.         if (target != null)
  43.         {
  44.             float step = _speed * Time.deltaTime; // calculate distance to move
  45.             transform.position = Vector3.MoveTowards(transform.position, target.position, step);
  46.  
  47.             if (Vector3.Distance(transform.position, target.position) < 0.001f)
  48.             {
  49.                 transform.position = Vector3.MoveTowards(transform.position, target.position, step);
  50.             }
  51.         }
  52.            
  53.        
  54.  
  55.  
  56.         if (transform.position.y >= 9.70f || transform.position.y <= -9.70)
  57.         {
  58.             Destroy(this.gameObject);
  59.         }
  60.         if (transform.position.x >= 14.5f || transform.position.x <= -14.5)
  61.         {
  62.             Destroy(this.gameObject);
  63.         }
  64.     }
  65.  
  66.     private void LaserMovement()
  67.     {
  68.         transform.Translate(Vector3.up * _speed * Time.deltaTime);
  69.  
  70.         if (transform.position.y >= 8f)
  71.         {
  72.             Destroy(this.gameObject);
  73.         }
  74.         else if (transform.position.y >= 8f && transform.parent.gameObject != null)
  75.         {
  76.             Destroy(transform.parent.gameObject);
  77.         }
  78.     }
  79.  
  80.     private void OnTriggerEnter2D(Collider2D other)
  81.     {
  82.         if (other.gameObject.CompareTag("1stBoss"))
  83.         {
  84.             Instantiate(_bossHitExplosionAnimPrefab, transform.position + new Vector3(0, 0.5f, 0), Quaternion.identity);        
  85.         }
  86.  
  87.         if (other.gameObject.CompareTag("BigEnemy"))
  88.         {
  89.             Instantiate(_bossHitExplosionAnimPrefab, transform.position + new Vector3(0, 0.5f, 0), Quaternion.identity);
  90.         }
  91.     }
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement