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!
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;
}
}