Hi everyone,
I’ve hit a bit of a wall with a project I’m working on. Essentially I am trying to make a basic AI for an enemy that retreats to the nearest one of it’s kind when it reaches a certain amount of health. I have a theory on how to carry this out but clearly the logic is flawed somewhere… hopefully you guys can help! I have a similar-ish code working in another part of the script. That code simply judges the distance between it and the player and when it within a certain range, it follows.
The part of the script that handles that aspect is the following:
public Transform Player;
public float MaxRange = 15f;
public float MinRange = 1.3f;
if((System.Convert.ToBoolean(Vector3.Distance(transform.position,
Player.position)<MaxRange)))
{
if((System.Convert.ToBoolean(Vector3.Distance(transform.position,
Player.position)>MinRange)))
{
State = demonState.Walking;
}
else
{
isAttacking = true;
State = demonState.Attacking;
}
and that works fine, however I am trying to re factor it into a function that loops through all the game objects in the world called demon and then picks one within a certain range to be it’s sort of backup. With that variable set I will then create a state where the enemy runs up to it and then does the required commands.Here is what I have attempted so far.
public Transform Backup;
for (int i = 0; i < 12; i++) // I'm using the number 12 because there are 12 of them in the game at the moment, I don't know the
//expression that encompasses all of a particular object yet... as you can tell, I'm learning this as I go :P
{
if((System.Convert.ToBoolean(Vector3.Distance(transform.position,
GameObject.Find("demon").transform.position)<MaxRange*2)))
{
Backup = GameObject.Find("demon").transform;
Debug.Log("Backup detected");
}
}
The funny thing about that code is that when I run it, it shows the debug message but it doesn’t set the variable. As I said before, I know the problem lies with me but I’m a little stuck. Any help would be appreciated ![]()