- Home /
Stop the movement to a direction
I am making a runner game. I have an enemy that is chasing the player. I've 2 raycasts on the enemy, one raycast to the left and one raycast to the right. If there are more than 1 chasing enemies then they will collide because they move to the same direction. My question is how can the enemy stop moving to the direction of the enemies because they may not collide. If the raycast hit an enemy then he needs to stop moving to that direction
Here is my code
RaycastHit hit;
Ray leftRay = new Ray(transform.position, Vector3.left);
Ray rightRay = new Ray(transform.position, Vector3.right);
Debug.DrawRay(transform.position, Vector3.left * enemyDistance);
Debug.DrawRay(transform.position, Vector3.right * enemyDistance);
if (!Hit)
{
if (Physics.Raycast(leftRay, out hit, enemyDistance))
{
if (hit.collider.tag == "Enemy")
{
Hit = true;
}
}
}
if (!Hit)
{
if (Physics.Raycast(rightRay, out hit, enemyDistance))
{
if (hit.collider.tag == "Enemy")
{
Hit = true;
}
}
}
Comment