- Home /
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! :)
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;
}
}
Answer by ExtinctSpecie · Mar 06, 2017 at 10:12 PM
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
Do I add the code right? Enemy spin very fast now
public float timer = 0.5f;
public float enemyWaitTime;
void Start ()
{
}
void Update () {
hittingWall = Physics2D.OverlapCircle (wallCheck.position, wallCheckRadius, whatIsWall);
notAtEdge = Physics2D.OverlapCircle (edgeCheck.position, edgeCheckRadius, whatIsWall);
if (hittingWall || !notAtEdge)
{
if (timer > 0)
{
timer -= Time.deltaTime;
return;
}
timer = 0.5f;
}
moveRight = !moveRight;
if (hittingWall || !notAtEdge)
{
if (timer > 0)
{
timer -= Time.deltaTime;
return;
}
moveRight = !moveRight;
timer = 0.5f;
}//put the moveRight = !moveRight inside if so the player can swap when he hits a wall and 0.5 seconds have passed
Can you please help with the Speed on this script? I want to make that animation transition which is working on speed But how can i make here that speed is go machimatically to 0 and backwards to my parameter when the enemy is waiting? How to make Timer set the Speed smoothly to 0 and backward? PLEASE if you know how, help me on this one!
Answer by jwulf · Mar 07, 2017 at 11:24 AM
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
.
Thank you very much Your answer is working too but it happened after 0.5f after detecting, not immediately)thank you