- Home /
Question by
Brain_not_found · Feb 10, 2019 at 03:45 PM ·
enemy aipatrol
Patrol for enemies not working correctly (2D)
Hi, I'm pretty new to Unity and I actually try to make a 2D Rogue-Like game.
I would to make my enemies to do some "patrolling" moves in random directions until their detection range equals to the distance between them and their spawnpoint. Then they stop and will wait for a random time before moving again in another random direction.
My problem is that enemies move correctly when they spawn until their detection range is reached, but then stop. They still can chase the player (classic zombie AI) and will return close to their spawnpoint if they can't find anymore the player...
I already looked on many sites for answers but couldn't find anything helpful
Here is the code with argumentation to explain it :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Animations;
using UnityEngine.UI;
public class EnemyScript : MonoBehaviour {
public float moveSpeed, rangeDetection; // rangeDetection has been set to 10 in the inspector
float randX, randY, randTime, nextMove = 0.0f;
Animator anim;
BoxCollider2D boxCollider;
Rigidbody2D rigBody;
private Transform player, spawnPoint;
bool verifPlayer = false, mouv = false; // mouv = mov for movement, but in french
public GameObject monstreTete, monstreTorse, monstreJambes, monstreBrasDroit, monstreBrasGauche;
// Use this for initialization
void Start ()
{
anim = GetComponent<Animator>();
rigBody = GetComponent<Rigidbody2D>();
boxCollider = GetComponent<BoxCollider2D>();
if (GameObject.FindGameObjectWithTag("Player"))
player = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
spawnPoint = GameObject.Find("EnemySpawner").GetComponent<Transform>();
Randomize(); // first time randomizing randX, randY and randTime
}
// Update is called once per frame
void Update()
{
if (player)
{
if (Vector3.Distance(transform.position, player.position) < rangeDetection) // At any update where the player is in range, the enemy will chase the player
{
rigBody.velocity = Vector2.zero; // Stop any previous rigBody modification
transform.position = Vector2.MoveTowards(transform.position, player.position, moveSpeed * Time.deltaTime);
}
else // If the player isn't in the enemy's detection range
{
if (Vector3.Distance(transform.position, spawnPoint.position) > rangeDetection) // If the enemySpawner is out of enemy's detection range
{
rigBody.velocity = Vector2.zero; // Stop any previous rigBody modification
transform.position = Vector2.MoveTowards(transform.position, spawnPoint.position, moveSpeed * Time.deltaTime); // Go to the direction of the enemySpawner until rangeDetection == Distance to the enemySpawner
}
else if (Vector3.Distance(transform.position, spawnPoint.position) <= rangeDetection) // If the enemySpawner is in enemy's detection range
{
if (Time.time > nextMove && !mouv) // If mouv has been set to false and the waiting time has expired
{
rigBody.velocity = new Vector2(randX, randY); // Move in randX and randY, making him move in random direction at random speed
mouv = true;
}
else if (mouv && Vector3.Distance(transform.position, spawnPoint.position) <= rangeDetection) // If mouv has been set to true and the enemySpawner is in the detection range
{
mouv = false;
rigBody.velocity = Vector2.zero; // Stop any previous rigBody modification
nextMove = Time.time + randTime; // Setting waiting time that has been previously randomized
Randomize();
}
}
}
}
void Randomize()
{
randX = Random.Range(-moveSpeed, moveSpeed);
randY = Random.Range(-moveSpeed, moveSpeed);
randTime = Random.Range(3f, 8f);
}
}
Thanks for anybody who will answer! :)
Comment