Hey everyone. I am working on a procedural 2d dungeon crawler and am currently trying to set up an AI random roaming mechanic for my enemy prefabs.
Currently the script basically picks a random number (random.range 0 - 4) and assigns a direction based on the number returned. it then does a raycasthit2d line cast to determine what is in the space it is moving into. once it determines it is ok to move into said space it then should move there however it will not currently complete the transform.position portion after determining the direction nor will it properly complete the yield return new after the random number generation so currently it calls the function on every frame. despite it calling on every frame however it still will not actually change the gameobject’s transform.
Here is the script in full for the sake of testing:
using System.Collections;
using UnityEngine;
public class EnemyAI : MonoBehaviour {
RaycastHit2D hit;
RaycastHit2D player;
public LayerMask blockingLayer;
public LayerMask playerLayer;
public Vector2 start;
public Vector2 end;
public Vector2 moveDirection;
public int moveNumber;
public bool canMove;
void Start()
{
canMove = true;
}
void Move()
{
if (canMove == true)
{
StartCoroutine(Mover());
canMove = true;
}
}
IEnumerator Mover()
{
moveNumber = Random.Range(0, 4);
if (moveNumber == 0)//Up
{
moveDirection = new Vector2(transform.position.x, transform.position.y + 1);
start = transform.position;
end = (start + moveDirection);
this.GetComponent<BoxCollider2D>().enabled = false;
hit = Physics2D.Linecast(start, end, blockingLayer); //Stores the results of the RayCastHit2D between the start Vector2 and end Vector2 ie. did it hit anything on the attempted move path.
player = Physics2D.Linecast(start, end, playerLayer);
this.GetComponent<BoxCollider2D>().enabled = true;
if (hit.transform == null)
{
gameObject.transform.position = (start + moveDirection);
canMove = false;
yield return new WaitForSecondsRealtime(1f);
}
}
if (moveNumber == 1)//Down
{
moveDirection = new Vector2(transform.position.x, transform.position.y - 1);
start = transform.position;
end = (start + moveDirection);
this.GetComponent<BoxCollider2D>().enabled = false;
hit = Physics2D.Linecast(start, end, blockingLayer); //Stores the results of the RayCastHit2D between the start Vector2 and end Vector2 ie. did it hit anything on the attempted move path.
player = Physics2D.Linecast(start, end, playerLayer);
this.GetComponent<BoxCollider2D>().enabled = true;
if (hit.transform == null )
{
gameObject.transform.position = (start + moveDirection);
canMove = false;
yield return new WaitForSecondsRealtime(1f);
}
}
if (moveNumber == 2)//Right
{
moveDirection = new Vector2(transform.position.x + 1, transform.position.y);
start = transform.position;
end = (start + moveDirection);
this.GetComponent<BoxCollider2D>().enabled = false;
hit = Physics2D.Linecast(start, end, blockingLayer); //Stores the results of the RayCastHit2D between the start Vector2 and end Vector2 ie. did it hit anything on the attempted move path.
player = Physics2D.Linecast(start, end, playerLayer);
this.GetComponent<BoxCollider2D>().enabled = true;
if (hit.transform == null )
{
gameObject.transform.position = (start + moveDirection);
canMove = false;
yield return new WaitForSecondsRealtime(1f);
}
}
if (moveNumber == 3)//Left
{
moveDirection = new Vector2(transform.position.x - 1, transform.position.y);
start = transform.position;
end = (start + moveDirection);
this.GetComponent<BoxCollider2D>().enabled = false;
hit = Physics2D.Linecast(start, end, blockingLayer); //Stores the results of the RayCastHit2D between the start Vector2 and end Vector2 ie. did it hit anything on the attempted move path.
player = Physics2D.Linecast(start, end, playerLayer);
this.GetComponent<BoxCollider2D>().enabled = true;
if (hit.transform == null )
{
gameObject.transform.position = (start + moveDirection);
canMove = false;
yield return new WaitForSecondsRealtime(1f);
}
}
}
void Update()
{
Move();
}
}