After the enemy collides with the player I want him to stop for some time, so it doesnt chase after him all the time. It looks weird. Like, add an Invoke function or something(not a pro). Please implement it right away (im just a beginner) Here is the code(I was following some 7 hour tutorial):
// Experience
public int xpValue = 1;
//Particle system
public GameObject effect;
// Logic
public float triggerLength = 1;
public float chaseLength = 5;
public bool chasing;
public bool collidingWithPlayer;
private Transform playerTransform;
private Vector3 startingPosition;
// Hitbox
public ContactFilter2D filter;
private BoxCollider2D hitbox;
private Collider2D[] hits = new Collider2D[10];
protected override void Start()
{
base.Start();
playerTransform = GameManager.instance.player.transform;
startingPosition = transform.position;
hitbox = transform.GetChild(0).GetComponent<BoxCollider2D>();
}
protected void FixedUpdate(){
//Is the player in range?
if(Vector3.Distance(playerTransform.position, startingPosition) < chaseLength)
{
if(Vector3.Distance(playerTransform.position, startingPosition) < triggerLength)
chasing = true;
if (chasing){
if(!collidingWithPlayer){
UpdateMotor((playerTransform.position - transform.position).normalized);
}
}
else{
UpdateMotor(startingPosition - transform.position);
}
}
else{
UpdateMotor(startingPosition - transform.position);
chasing = false;
}
//Check for overlap
collidingWithPlayer = false;
boxCollider.OverlapCollider(filter, hits); // null reference expection
for (int i = 0; i < hits.Length; i++)
{
if (hits[i] == null)\
continue;
if(hits[i].tag == "Fighter" && hits[i].name == "Player"){
collidingWithPlayer = true;
}
//this array is not cleaned up, so we do it ourself
hits[i] = null;
}
}