0

In my scene, I have an agent which moves to a specific destination. As soon I try to duplicate it in order to create a second agent the resulting behavior is quite strange: the agents get transported to opposite corners of the map and stop moving. Also, the navigation mesh at runtime seems altered (as you can see in the second screenshot). As soon I disable one agent the other start working as expected.

EDIT: This is the code I'm using for the character (as you can see I manage the character rotation/translation manually instead of delegating the agent, which works well till I assign the same script to a second character as mentioned before).

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class AI_PlayerController : MonoBehaviour
{
    public Transform goal;
    bool isTracking = false;
    public float speed = 1.0f;
    Vector3 currentDirection;

    Animator anim;    
    NavMeshAgent agent;
    Vector2 smoothDeltaPosition = Vector2.zero;
    Vector2 velocity = Vector2.zero;

    // Start is called before the first frame update
    void Start()
    {
        anim = GetComponent<Animator>();
        agent = GetComponent<NavMeshAgent>();
        agent.SetDestination(goal.position);
        agent.updatePosition = false;
        agent.updateRotation = false;
    }
void Update ()
    {
        if (agent.remainingDistance > agent.radius) {
            Vector3 worldDeltaPosition = agent.nextPosition - transform.position;        
            worldDeltaPosition.y = 0;
            Quaternion rotation = Quaternion.LookRotation(worldDeltaPosition);
            transform.rotation = Quaternion.Slerp(transform.rotation, rotation, .1f);
            anim.SetBool("move", true);
        } else {
            anim.SetBool("move", false);
        }

    }

    void OnAnimatorMove ()
    {
        // Update position to agent position
        transform.position = agent.nextPosition;
    }
}

Only one active agent Both duplicated agent active

Claus
  • 5,662
  • 10
  • 77
  • 118

1 Answers1

1

Did you clear and rebake the navmesh? I'm not sure if that will work but it might solve the problem. Otherwise try removing and re-adding the NavMeshAgent component on both of them. If they are prefabs then you might want to re-create them instead of copying them so that unwanted changes don't propagate between them accidentally.

Edit: It's difficult to determine the problem without seeing your code. Could you upload the navigation/movement script?

Edit 2 Could you try replacing the script on both characters with the following movement script? The animation logic can be added back in later.

If this doesn't work then I would recommend checking that the NavMesh is set up correctly in your scene (for example, making sure that navigation obstacles are marked correctly, and clearing and rebaking the NavMesh).

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

[RequireComponent(typeof(NavMeshAgent))]
public class AI_PlayerController : MonoBehaviour
{
    public Transform goal;
    public float speed = 3.0f;
    public float acceleration = 8.0f;

    NavMeshAgent agent;

    // Start is called before the first frame update
    void Start()
    {
        agent = GetComponent<NavMeshAgent>();
        agent.speed = speed;
        agent.acceleration = acceleration;
        agent.SetDestination(goal.position);
    }
}
Nicholas Bucher
  • 440
  • 2
  • 7
  • Yes, I have tried to re-bake and remove/add the NavMeshAgent but nothing changed. I have added the source code to the question. Thanks – Claus May 10 '19 at 15:51
  • Ok will try this variation and let you know – Claus May 15 '19 at 09:07