Flee when low on health

Hi everyone, i am trying to make an underwater game using only sea creatures. so far i have my AI purse and attacking each other. I have a health and damage script but what i am trying to achieve is to make the AI shark flee when it’s health drops to a certain level. Would anyone be able to help in starting off this ? i can posts my scripts if needed. I am not using Navmesh at the moment I am using Mercuna.

What aspect are you having trouble with? Detecting if the health is low, or how to flee?

both. It’s health goes down when hit. I can get it to flee upon detection but ideally I only want it to flee when it’s health is low

I assume you have some sort of health script already, with some sort of health value and maximum health value as well? If you divide the health by the max health, it will give you a number between 0 and 1 where 1 is full, 0 is none, 0.5 is half, etc.
Then you could define low health as:
(health/max_health) <= 0.2f
change that 0.2 to whatever you want. This script comparison will check if the health is equal or less than 20%.

where i would i implement the health/max_health onto my health script ?

my health script

public ParticleSystem deathParticles;

    Animator _anim;
    public string MyTrigger;
    public int maxHealth;
    public int currentHealth;
    public Rigidbody m_rigidBody;
    public GameObject bloodEffect;

    void Start()
    {
        currentHealth = maxHealth;
        m_rigidBody = GetComponent<Rigidbody>();
        _anim = GetComponent<Animator>();
    }


    void Update()
    {

    }

    public void DamagePlayer(int Hurt, Vector3 direction)
    {
        currentHealth -= Hurt;
        if (currentHealth <= 50)
        {
            currentHealth = 0;
            {
                _anim.SetTrigger(MyTrigger);

            }


            Instantiate(bloodEffect, transform.position, Quaternion.identity);
            m_rigidBody.isKinematic = true;
            Instantiate(deathParticles, transform.position, Quaternion.identity);
            Destroy(gameObject);
        }

    }

What is the script for your AI?

what the health or damage script?

At the moment i want AI to Attack AI. So their is no player at the moment. (Creating an ecosystem)

You said that you had implemented pursuing a target already, right? You will need to modify that script so that you can switch between your pursuing behavior and your fleeing behavior.

I am using a behavior tree to allow pursue and flee so how would i edit the script to allow this as so far my shark (see object, pursue then attack and back to wander)

In that case you want to modify your behavior tree so that you can switch from one to the other. Generally the purposes of a behavior tree is chose which behavior to use at any given time based on conditions, so this is exactly the sort of thing you want to add to your behavior tree. Are you using Opsive Behavior Designer, by any chance?

im not no i was thinking about getting it but i wouldn’t know how to switch from say low health flee ?

Behavior Designer is the one that I have experience with. Which implementation do you have?

panda free but looking through behavior designer so will get that one but as that contains pursue and flee but how would i integrate my health script into this to allow fleeing when low health?

Ah, ok. Fortunately, I’ve looked at Panda BT before too, when I was evaluating different options. Check out this example in the documentation:

http://www.pandabehaviour.com/?page_id=23#Implementing_tasks_in_C

So using this example, we can easily add a Panda BT task to your health script. It would look like this:

[TASK]
void CheckLowHealth ()
{ if ((((float)currentHealth)/((float)maxHealth))<=0.2 )
  {
      Task.current.Succeed();
  }
  else
  {
      Task.current.Fail();
  }
}

Then you can use that check in your behavior tree.

and how would i be able to do it if i went with behavior designer?

Two ways that I can think, of:

Behavior Designer has it’s own template for creating your own task, just like Panda does. Behavior Designer’s is more complicated though and you have to inherit from a specific class, so you won’t be able to add it to your already-existing Monobehaviour like you can with Panda.

The more useful way is to use a special type of node that Behavior Designer provides that can receive an event. I that case you could write the same sort of conditional code like above and then call a special event in the behavior tree.

It looks like Panda is probably easier for this situation.

Ah right so i would have change the health and damage scripts make then new conditional tasks?

If you’re talking about Behavior Designer, I’d say that you wouldn’t change the health and damage scripts to conditional tasks. You’d still implement the damage and health the same way, but you would make additional scripts to act as conditional tasks.

I have implemented a health system before (similar to yours) and I opted to use the events instead, though.

ah right but what would go into the conditional task scripts ?

Well, if I were implementing this with a Behavior Designer condition task, I would first add a convenient method to your health script, something like:

public bool hasLowHealth ()
{ 
   return ((((float)currentHealth)/((float)maxHealth))<=0.2 );
}

Then, in your custom task, you could get a reference to your health script in the OnAwake function. The Behavior Designer Condition class has a GetComponent, just like mono behaviour.

In the OnUpdate function, it would look similar to the Panda BT example:

public override TaskStatus OnUpdate
{ 
   if (myHealthScript.hasLowHealth())
   { return (TaskStatus.Success);
   }
  else
  { return (TaskStatus.Failure);
  }
}