Unity Steering behaviours and Behaviour Trees

112 Views Asked by At

I’m trying to implement AI system in unity using steering behaviours and Behaviour Trees as part of a project. I have most steering behaviour that work properly and the Behaviour tree as well but currently they are seperated. My agent behaviour tree, what it does currently, is move to a enemy location and perform some attacks. However I wanted to create a new class inside of the Agent BT to make the agent wander before finding the enemy and engaging in combat. However I’m not sure what’s the best approuch to call the Wander script inside of the agent BT and execute it.

This is one of the classes inside of the Agent BT that handles the movement towards the enemy

public class AgentMoveToEnemy : BTNode
{
    private AgentBB aBB;
    private Agent agentRef;
    bool firstRun = true;

    public AgentMoveToEnemy(Blackboard bb, Agent agent) : base(bb)
    {
        aBB = (AgentBB)bb;
        agentRef = agent;
    }

    public override BTStatus Execute()
    {
        if (firstRun)
        {
            firstRun = false;
            Debug.Log("Moving to enemy");
        }
        BTStatus rv = BTStatus.RUNNING;
        if ((agentRef.transform.position - aBB.enemyLocation).magnitude <= 10.0f)
        {
            agentRef.AgentMoveTo(aBB.enemyLocation);

        }

        if ((agentRef.transform.position - aBB.enemyLocation).magnitude <= 1.0f)
        {
            Debug.Log("Reached the enemy");
            rv = BTStatus.SUCCESS;
            firstRun = true;
        }
        return rv;
    }

And this is my wander behaviour

public class Wander : Steering
{
    [SerializeField] private float wanderRate = 0.4f;
    [SerializeField] private float wanderOffset = 1.5f;
    [SerializeField] private float wanderRadius = 4f;

    private float wanderOrientation = 0f;

    private float RandomBinomial()
    {
        return Random.value - Random.value;
    }

    private Vector3 OrientationToVector(float orientation)
    {
        return new Vector3(Mathf.Cos(orientation), 0, Mathf.Sin(orientation));
    }

    public override SteeringData GetSteering(SteeringBehaviours steeringBehaviours)
    {
        SteeringData steering = new SteeringData();
        wanderOrientation += RandomBinomial() * wanderRate;

        float characterOrientation = transform.rotation.eulerAngles.y * Mathf.Deg2Rad;
        float targetOrientation = wanderOrientation + characterOrientation;

        Vector3 targetPosition = transform.position + (wanderOffset * OrientationToVector(characterOrientation));
        targetPosition += wanderRadius * OrientationToVector(targetOrientation);

        steering.linear = targetPosition - transform.position;
        steering.linear.Normalize();
        steering.linear *= steeringBehaviours.maxAcceleration;
        

        return steering;
    }
}

I tried to get the Wander behaviour through: agentRef.AddComponent<Wander> however only adds the component it doesn't actually do anything else even when I use the steering Behaviour script that handles the steering forces,

Can someone help me?

0

There are 0 best solutions below