Enemy Patrol Wait Time

Hi, I’m looking for create an Enemy Patrol AI script

And I want to make him to Stop for several seconds on the edge of the platform or when he is detect the wall in front of him
So I made the wall and edge detection check for him and he’s moving quiet right. Check it in my video.

Enemy walking video on youtube

But I want to make him wait and stay for couple seconds in the moment when he detect the edge or wall
I tried the WaitForSecond method but i’m not quiet sure how it works and made the float “Enemy Wait Time”

If you can please help me to solve this! :slight_smile:

Here is the Code

using UnityEngine;
using System.Collections;

public class EnemyPatrol : MonoBehaviour {

	public float moveSpeed;
	public bool moveRight;

	public Transform wallCheck;
	public float wallCheckRadius;
	public LayerMask whatIsWall;
	private bool hittingWall;

	private bool notAtEdge;
	public Transform edgeCheck;

	public float enemyWaitTime;

	void Start () 
	{
		
	}
		
	void Update () {

		hittingWall = Physics2D.OverlapCircle (wallCheck.position, wallCheckRadius, whatIsWall);

		notAtEdge = Physics2D.OverlapCircle (edgeCheck.position, wallCheckRadius, whatIsWall);

		if(hittingWall || !notAtEdge)
			moveRight = !moveRight;
		
		if (moveRight) 
		{ 
			transform.localScale = new Vector3 (0.75f, 0.75f, 1f);
			GetComponent<Rigidbody2D>().velocity = new Vector2(moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
		} else {
			transform.localScale = new Vector3 (-0.75f, 0.75f, 1f);
			GetComponent<Rigidbody2D>().velocity = new Vector2(-moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
		}
	}

	IEnumerator WaitTime() {
		moveSpeed = 0;
		yield return new WaitForSeconds (enemyWaitTime);
		yield return 0;
	}
}

float timer = 0.5f;
if(hittingWall)
{
if(timer>0)
{
timer -= Time.deltaTime;
return;
}
timer = 0.5f;
}
//we reset the timer again notice that this will execute only when hittingWall = true
//so we reset the timer and it counts down again when hittingWall = true

You never call WaitTime(), do you?

You should update this

     if(hittingWall || !notAtEdge)
         moveRight = !moveRight;

to

     if(hittingWall || !notAtEdge) {
         moveRight = !moveRight;
         StartCoroutine(WaitTime());
     }

Further introduce a bool “moving” somewhere in the class:

private bool moving = true; // defaults to true, so the enemy moves at the beginning

And change WaitTime() to:

 IEnumerator WaitTime() {
     moving = false;
     yield return new WaitForSeconds (enemyWaitTime);
     moving = true;
 }

And at the start of Update():

void Udate() {
    if(!moving)
        return;
} 

, so the enemy doesn’t move when moving == false.

There are some other things I would suggest to change in your code, but only to name the most importent: Calling GetComponent() in Update() (i.e. every frame) several times is not a good idea. You should rather make the rigidBody a member variable and introduce an Awake()-Method…

RigidBody2D rb;
void Awake() {
    rb = GetComponent<RigidBody2D>();
}

… and then, whenever you want to change something on the RigidBody2D, you just call

rb.velocity = ...; // or whatever

.