using System.Collections; using System.Collections.Generic; using UnityEngine; public class jetEnemy : MonoBehaviour { public float speed = 60; public float rotSpeed = 100; public Transform playerTransform; public GameObject explosion; public float retreatDis; public float attackDis; public int state; // 0 - attack | 1 - go away private bool shotMissle; public float attackDistance; private float TimeBtwSpawn; public float StartTimeSpawn; public GameObject missleLauncher; public GameObject missle; private Camera cam; public GameObject missleDrop; public float targetUItimer; public GameObject playerMissle; public bool chased; // Start is called before the first frame update void Start() { playerTransform = GameObject.FindWithTag("Player").transform; TimeBtwSpawn = StartTimeSpawn; targetUI = Instantiate(targetUI); targetUI.transform.SetParent(GameObject.FindGameObjectWithTag("Canvas").transform); if (playerTransform.gameObject.GetComponent().missleEnabled) ShowTargeting(); targetUI.GetComponent().enemy = transform; cam = GameObject.FindGameObjectWithTag("MainCamera").GetComponent(); targetUI.SetActive(false); } // Update is called once per frame void Update() { float dist = Vector3.Distance(playerTransform.position, transform.position); if(state == 0) { Vector3 direction = playerTransform.position - transform.position; Quaternion rotation = Quaternion.LookRotation(direction); transform.rotation = Quaternion.Lerp(transform.rotation, rotation, rotSpeed * Time.deltaTime); if(dist <= attackDistance && !shotMissle) { ShootMissle(); shotMissle=true; } }else if(state == 1) { Vector3 direction = transform.position - playerTransform.position; Quaternion rotation = Quaternion.LookRotation(direction); transform.rotation = Quaternion.Lerp(transform.rotation, rotation, rotSpeed * Time.deltaTime * 0.5f); if (dist >= attackDis) { shotMissle = false; state = 0; } } transform.Translate(0, 0, speed * Time.deltaTime); if (targetUI != null) targetUI.transform.position = cam.WorldToScreenPoint(transform.position); if (targetUI.activeSelf) { if (targetUItimer > 0) targetUItimer -= Time.deltaTime; else targetUI.SetActive(false); } if (chased && playerMissle == null) { targetUI.GetComponent().localScale = new Vector3(2f, 2f, 2f); HideTargeting(); chased = false; } } void ShootMissle() { Instantiate(missle, missleLauncher.transform.position, missleLauncher.transform.rotation); state = 1; } private void OnTriggerEnter(Collider other) { if (other.CompareTag("missle")) { Instantiate(explosion, transform.position, transform.rotation); int i = Random.Range(0,3); if(i ==0) Instantiate(missleDrop, transform.position, transform.rotation); Destroy(gameObject, 0); Destroy(targetUI); } if (other.CompareTag("Player")) { Instantiate(explosion, transform.position, transform.rotation); int i = Random.Range(0, 3); if (i == 0) Instantiate(missleDrop, transform.position, transform.rotation); Destroy(gameObject, 0); Destroy(targetUI); } } public GameObject targetUI; public void ShowTargeting() { Debug.Log("Targeting showed"); targetUI.SetActive(true); } public void HideTargeting() { Debug.Log("Targeting hiden"); targetUI.SetActive(false); } public void ChasedByPlayerMissle(GameObject playerMissle) { this.playerMissle = playerMissle; chased = true; } }