Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class HomingPlayerLaser : MonoBehaviour
- {
- [SerializeField] private float _speed = 8f;
- [SerializeField] private GameObject _bossHitExplosionAnimPrefab;
- [SerializeField] private Transform target; // enemyTransform
- SpawnManager _spawnManager;
- // Use this for initialization
- void Start()
- {
- _spawnManager = GameObject.Find("SpawnManager").GetComponent<SpawnManager>();
- if (target != null) // but it IS NULL at start
- {
- target = GameObject.FindGameObjectWithTag("Enemy").transform;
- target = _spawnManager._enemyPrefab.transform;
- }
- }
- // Update is called once per frame
- void Update()
- {
- if (target == null)
- {
- transform.Translate(Vector3.up * _speed * Time.deltaTime);
- }
- if (target != null)
- {
- float step = _speed * Time.deltaTime; // calculate distance to move
- transform.position = Vector3.MoveTowards(transform.position, target.position, step);
- if (Vector3.Distance(transform.position, target.position) < 0.001f)
- {
- transform.position = Vector3.MoveTowards(transform.position, target.position, step);
- }
- }
- if (transform.position.y >= 9.70f || transform.position.y <= -9.70)
- {
- Destroy(this.gameObject);
- }
- if (transform.position.x >= 14.5f || transform.position.x <= -14.5)
- {
- Destroy(this.gameObject);
- }
- }
- private void LaserMovement()
- {
- transform.Translate(Vector3.up * _speed * Time.deltaTime);
- if (transform.position.y >= 8f)
- {
- Destroy(this.gameObject);
- }
- else if (transform.position.y >= 8f && transform.parent.gameObject != null)
- {
- Destroy(transform.parent.gameObject);
- }
- }
- private void OnTriggerEnter2D(Collider2D other)
- {
- if (other.gameObject.CompareTag("1stBoss"))
- {
- Instantiate(_bossHitExplosionAnimPrefab, transform.position + new Vector3(0, 0.5f, 0), Quaternion.identity);
- }
- if (other.gameObject.CompareTag("BigEnemy"))
- {
- Instantiate(_bossHitExplosionAnimPrefab, transform.position + new Vector3(0, 0.5f, 0), Quaternion.identity);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement