0
\$\begingroup\$

I have items on the ground which I can pick up. If I'm within the radius of a trigger collider around the item, I just pick it up. If i'm not, I want to set my NavMeshAgent's destination to the item and then pick it up once I've arrived. I have done this exact thing before but have since completely forgotten how to do it.

The way picking up an item occurs is by clicking on its UI nameplate, not the physical item gameObject. Therefore, I'm using the OnPointerClick function. If possible I'd like to not have to check every frame whether I have arrived at my destination or not, but I'm open to doing it that way for now.

I'm not really familiar with how the NavMeshAgent API works. I would think that if (!agent.pathPending && agent.remainingDistance <= agent.stoppingDistance) or something similar would work. I'm kind of lost though, any help is appreciated.

void Update()
{
    if (!navMeshagent.pathPending && navMeshagent.remainingDistance <= navMeshagent.stoppingDistance)
    {
        PickUpItem();
    }
}

public void OnPointerClick(PointerEventData eventData)
{
    if (itemPickupRange.playerIsInRange)
    {
        PickUpItem();
    }
    else
    {
        navMeshAgent.SetDestination(item.transform.position);
    }
}

This doesn't work. I've tried breaking up the conditions in Update and making it a nested loop, and it still doesn't work. One other thing: there's some weird behaviour with remainingDistance. Sometimes It's a number between 0 and 1 when I'm positive that the actual distance must be much larger.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.