Guest User

Untitled

a guest
Aug 23rd, 2022
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5.  
  6.  
  7.  
  8. public class Enemy : MonoBehaviour
  9. {
  10. [SerializeField]
  11. private GameObject _explosionPre;
  12.  
  13. [SerializeField]
  14. private GameObject[] _cities;
  15. [SerializeField]
  16. private float _enemySpeed=0.25f;
  17. // Start is called before the first frame update
  18. void Start()
  19. {
  20.  
  21. }
  22.  
  23. // Update is called once per frame
  24. void Update()
  25. {
  26. FallToCity();
  27. }
  28.  
  29. //falll diagonally to city gameobject
  30. public void FallToCity()
  31. {
  32. int randomCity = Random.Range(0, _cities.Length-1);
  33. Vector3 destinationPos = _cities[randomCity].transform.position;
  34. Vector3 currentPos = transform.position;
  35. Vector3 direction = destinationPos - currentPos;
  36. transform.Translate(direction*Time.deltaTime*_enemySpeed);
  37. }
  38. private void OnTriggerEnter(Collider other)
  39. {
  40. if (other.gameObject.tag == "City")
  41. {
  42. Destroy(other.gameObject);
  43. Instantiate(_explosionPre, transform.position, Quaternion.identity);
  44. Destroy(gameObject);
  45.  
  46. }
  47. }
  48. }
  49.  
Advertisement
Add Comment
Please, Sign In to add comment