using System.Collections; using System.Collections.Generic; using UnityEngine; public class EnemyController : MonoBehaviour, IPooledObject{ public float speed; public float maxSpeed; public Material red; public GameObject[] materialsApplied; public LayerMask whatStops; public Transform circlePos; bool canDisappear; Transform target; Rigidbody2D rb2d; Vector2 mov; Animator anim; // Start is called before the first frame update void Start(){ rb2d = GetComponent(); target = GameObject.FindGameObjectWithTag("Player").transform; anim = GetComponentInChildren(); anim.SetBool("Walking", true); } public void OnObjectSpawn(){ canDisappear = false; } void FixedUpdate(){ /*if (target.position.x < transform.position.x){ rb2d.AddForce(Vector2.left * speed * Time.deltaTime); }else{ rb2d.AddForce(Vector2.right * speed * Time.deltaTime); } if (target.position.y < transform.position.y){ rb2d.AddForce(Vector2.down * speed * Time.deltaTime); }else{ rb2d.AddForce(Vector2.up * speed * Time.deltaTime); } float limitedSpeed = Mathf.Clamp(rb2d.velocity.x, -maxSpeed, maxSpeed); float yLimitedSpeed = Mathf.Clamp(rb2d.velocity.y, -maxSpeed, maxSpeed); rb2d.velocity = new Vector2(limitedSpeed, yLimitedSpeed);*/ mov = transform.position; mov = Vector2.MoveTowards(rb2d.position, target.position, speed * Time.fixedDeltaTime); rb2d.MovePosition(mov); //print(rb2d.velocity.magnitude); } void OnTriggerEnter2D(Collider2D col){ if(col.gameObject.tag == "Attack"){ anim.SetBool("Attacking", true); for (int i = 0; i < materialsApplied.Length; i++){ materialsApplied[i].GetComponent().material = red; } } } void OnBecameVisible(){ canDisappear = true; } void OnBecameInvisible(){ if (canDisappear){ gameObject.SetActive(false); } } }