0

Okay so for a week I'm trying to get this problem solved. I have a navmesh agent and it has a nav obstacle in it too, so it enables/disables this component in such a way other agents in the scene don't push him around. When other agents that you controll reach its destination they become an obstacle and when I click with mouse somewhere in the scene, it goes to a navmesh agent and starts to move again.

But now I have the problem that when I click, it doesnt switch to navmesh agent if its far from the player/object, sometimes it just dies and does not switch at all. And all agents are controlled by you, there is no enemy here that follows you .

Here is the code :

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

public class moveTest : MonoBehaviour {

    NavMeshAgent navAgent;
    NavMeshObstacle obstacle;

    // Use this for initialization
    void Start() {
        navAgent = GetComponent<NavMeshAgent>();
        obstacle = GetComponent<NavMeshObstacle>();
    }

    // Update is called once per frame
    void Update() {

        move();
        Stopped();

    }

    void Stopped()
    {
        if (navAgent.remainingDistance <= navAgent.stoppingDistance)
        {
            obstacle.enabled = true;
            navAgent.enabled = false;
        }
    }

    void move()
    {
        RaycastHit hit;
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Input.GetMouseButtonDown(1))
        {
            obstacle.enabled = false;
            navAgent.enabled = true;

            if (Physics.Raycast(ray, out hit, 10000))
            {
                navAgent.destination = hit.point;
                //navAgent.SetDestination(hit.point);
            }
        }
    }
}
Ignacio Alorre
  • 7,307
  • 8
  • 57
  • 94
Zid Lyx
  • 129
  • 2
  • 12
  • Possible duplicate of [How to avoid two NavMeshAgent push away each other in Unity?](https://stackoverflow.com/questions/23451983/how-to-avoid-two-navmeshagent-push-away-each-other-in-unity) – TheSkimek Oct 06 '17 at 13:08
  • doesnt fix the problem – Zid Lyx Oct 06 '17 at 13:11
  • Follow the link provided in the answer to achieve what you want. – TheSkimek Oct 06 '17 at 13:12
  • yea i have seen it before it doesnt fix anything its for a enemy that follows goes after a player and here we are talking about only a player – Zid Lyx Oct 06 '17 at 13:13
  • You want multiple agents to get near the player (or the mouseclick position) without them pushing eachother right? Thats exactly what is explained in the llink: `Basically what we are doing is this - if we have an agent which is in attack range, we want him to stay in one place, so we make him an obstacle by enabling the NavMeshObstacle component and disabling the NavMeshAgent component. ` – TheSkimek Oct 06 '17 at 13:23
  • yea but not in attack range just on the reached destination but the problem now is that on the start its a obstacle and wen i click on the terrain it wont move i stays in one place i will show you a gif here http://gph.is/2ggZpJq – Zid Lyx Oct 06 '17 at 13:41
  • @ZidLyx you are getting error in the console. Did you try fixing that first by doing something like this?: if(navAgent.enabled) Stopped(); – ZayedUpal Oct 06 '17 at 17:59
  • tnx your great that hels alot ^^ – Zid Lyx Oct 07 '17 at 07:00

0 Answers0