using System.Collections; using System.Collections.Generic; using UnityEngine; public class EnemyPatrol : MonoBehaviour { [HideInInspector] public bool pinned = false; [Header("Horizontal")] public bool isHorizontal; public float rangeHorizontal; public float xLeft; public float xRight; public Vector2 movementVectorHorizontal = Vector2.right; [Header("Vertical")] public bool isVertical; public float rangeVertical; public float yUp; public float yDown; public Vector2 movementVectorVertical = Vector2.up; [Header("General")] public float moveSpeed; Animator animator; void Awake() { animator = GetComponent(); } // Start is called before the first frame update void Start() { xLeft = transform.position.x - rangeHorizontal; xRight = transform.position.x + rangeHorizontal; yDown = transform.position.y - rangeVertical; yUp = transform.position.y + rangeVertical; movementVectorHorizontal *= moveSpeed; movementVectorVertical *= moveSpeed; } // Update is called once per frame void Update() { if (!pinned) { if (isHorizontal) { if (transform.position.x >= xRight || transform.position.x <= xLeft) { movementVectorHorizontal = -movementVectorHorizontal; GetComponent().flipX = !GetComponent().flipX; } transform.position += new Vector3(movementVectorHorizontal.x, 0, 0) * Time.deltaTime; } if (isVertical) { if (transform.position.y >= yUp || transform.position.y <= yDown) { movementVectorVertical = -movementVectorVertical; } transform.position += new Vector3(0, movementVectorVertical.y, 0) * Time.deltaTime; } } } public void TogglePin(bool isPinned) { if (isPinned) { animator.speed = 0; } else { animator.speed = 1; } } public void DefeatEnemy() { StartCoroutine(Defeat()); } IEnumerator Defeat() { this.gameObject.GetComponent().enabled = false; animator.Play("Explosion"); this.gameObject.GetComponent().Play(); yield return new WaitForSeconds(animator.GetCurrentAnimatorStateInfo(0).length); this.gameObject.GetComponent().enabled = true; this.gameObject.SetActive(false); } void OnDrawGizmos() { Gizmos.color = Color.green; if (isHorizontal){ Gizmos.DrawLine(new Vector3(transform.position.x - rangeHorizontal, transform.position.y, transform.position.z), new Vector3(transform.position.x + rangeHorizontal, transform.position.y, transform.position.z)); } if (isVertical) { Gizmos.DrawLine(new Vector3(transform.position.x, transform.position.y - rangeVertical, transform.position.z), new Vector3(transform.position.x, transform.position.y + rangeVertical, transform.position.z)); } } }