Monday, July 4, 2022
HomeGame Developmentunity - (Fluid Conduct Tree) Random NavMesh place returning the identical place,...

unity – (Fluid Conduct Tree) Random NavMesh place returning the identical place, as a substitute of calculating a brand new one


I’ve a NavMesh agent that wanders round each x seconds, strikes to a random place, and lastly it repeats the sequence. I made a take a look at utilizing a coroutine, which was known as by a context menu methodology.

Nonetheless, after I reimplemented the logic in a conduct tree, the random level was solely generated the primary time. Within the following turns of the sequence, the random level was all the time the identical. I am utilizing this library, known as Fluid Conduct Tree. I am actually undecided what’s fallacious, because the logic from the coroutine and the logic from the nodes of the tree is kind of actually the identical.

The code from the Coroutine:

CheckForSafeDestinationPoint(); // Caculates the random level and units it because the vacation spot of the agent

// wait till the trail is prepared
whereas (agent.pathPending)
{
    yield return null;
}

if (agent.pathStatus == NavMeshPathStatus.PathComplete)
{
    Debug.Log("Path is legitimate");
    agent.isStopped = false;

    whereas (agent.GetPathRemainingDistance() > agent.stoppingDistance)
    {
        // Wait till the agent reaches its vacation spot
        Debug.Log("Wandering");
        yield return null;
    }
}

yield break;

The code from the node of the tree (a leaf node known as AgentDestination):

protected override void OnStart()
{
    // Actually the identical as CheckForSafeDestinationPoint(), with the exception that the random level (the Vacation spot) is first calculated on the dad or mum tree a handed as a parameter to this node
    SetDestinationPoint(Vacation spot);
}

// This code is executed on the Replace methodology
protected override TaskStatus OnUpdate()
{
    whereas (agent.pathPending)
    {
        return TaskStatus.Proceed;
    }

    if (agent.pathStatus == NavMeshPathStatus.PathComplete)
    {
        agent.isStopped = false;

        if (agent.GetPathRemainingDistance() > agent.stoppingDistance)
        {
            // That is the equal of yield return null
            return TaskStatus.Proceed;
        }
    }

    // As soon as the node succeeded, the tree leaves this node
    return TaskStatus.Success;
}

The earlier logic is later carried out trough the tree:

void Awake()
{
    agent = GetComponent<NavMeshAgent>();

    tree = new BehaviorTreeBuilder(gameObject)
            .Sequence()
                .Wait(waitTicks)
                // Right here we go within the random calculated level as a parameter
                .AgentDestination("RandomDestination", ComputeRandomDestination())
            .Finish()
        .Construct();
}

void Replace()
{
    tree.Tick();
}

I’ve my theories, although I am not completely certain about them. First, I believe that each time I name the coroutine, a brand new occasion is created, thus creating with it a brand new random level, as anticipated. Nonetheless, I could also be complicated the occasion creating with the string-based name of the coroutine. Because the tree is created on the Awake() methodology, the ComputeRandomDestination might be getting cached one way or the other. Nonetheless, I believe this does not make a whole lot of sense because the agent can observe a transferring goal, which implies that it might calculate a brand new path each body, so in concept, it needs to be able to calculating a random level greater than as soon as, proper?

Anyway, level is, I do not know what’s fallacious. All I do know is that the leaf node AgentDestination is, after the primary flip, all the time going to the identical spot. It isn’t calculating a brand new random level. The node is in truth exiting, so evidently it is working as anticipated.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments