Advertisement
SizilStank

Untitled

Feb 26th, 2020
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.23 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.         target = GameObject.FindGameObjectWithTag("Enemy").transform;
  23.         target = _spawnManager._enemyPrefab.transform;
  24.        
  25.     }
  26.  
  27.     // Update is called once per frame
  28.     void Update()
  29.     {
  30.  
  31.         float step = _speed * Time.deltaTime; // calculate distance to move
  32.         transform.position = Vector3.MoveTowards(transform.position, target.position, step);
  33.  
  34.         if (Vector3.Distance(transform.position, target.position) < 0.001f)
  35.         {
  36.             transform.position = Vector3.MoveTowards(transform.position, target.position, step);
  37.         }
  38.  
  39.            
  40.        
  41.  
  42.  
  43.         if (transform.position.y >= 9.70f || transform.position.y <= -9.70)
  44.         {
  45.             Destroy(this.gameObject);
  46.         }
  47.         if (transform.position.x >= 14.5f || transform.position.x <= -14.5)
  48.         {
  49.             Destroy(this.gameObject);
  50.         }
  51.     }
  52.  
  53.     private void LaserMovement()
  54.     {
  55.         transform.Translate(Vector3.up * _speed * Time.deltaTime);
  56.  
  57.         if (transform.position.y >= 8f)
  58.         {
  59.             Destroy(this.gameObject);
  60.         }
  61.         else if (transform.position.y >= 8f && transform.parent.gameObject != null)
  62.         {
  63.             Destroy(transform.parent.gameObject);
  64.         }
  65.     }
  66.  
  67.     private void OnTriggerEnter2D(Collider2D other)
  68.     {
  69.         if (other.gameObject.CompareTag("1stBoss"))
  70.         {
  71.             Instantiate(_bossHitExplosionAnimPrefab, transform.position + new Vector3(0, 0.5f, 0), Quaternion.identity);        
  72.         }
  73.  
  74.         if (other.gameObject.CompareTag("BigEnemy"))
  75.         {
  76.             Instantiate(_bossHitExplosionAnimPrefab, transform.position + new Vector3(0, 0.5f, 0), Quaternion.identity);
  77.         }
  78.     }
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement