1

I'm working on a RTS game. I did a normal move script and I increase the agent's stopping distance so they don't look at each other and pump that much. But they still hit each other.

I can't find the way to make agents avoid each other and not push each other. Or someway to ignore the physics while still trying to avoid each other. Here is the code for the click to move

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

public class moveTest : MonoBehaviour {

    NavMeshAgent navAgent;
    public bool Moving;

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

    // Update is called once per frame
    void Update () {
        move();
    }

    void move()
    {
        RaycastHit hit;
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Input.GetMouseButtonDown(1))
        {
            Moving = true;
            if (Moving)
            {
                if (Physics.Raycast(ray, out hit, 1000))
                {
                    navAgent.SetDestination(hit.point);
                    navAgent.Resume();
                }
            }
        }
    }
}
CDspace
  • 2,639
  • 18
  • 30
  • 36
Zid Lyx
  • 129
  • 2
  • 12

1 Answers1

2

From the following link (which sadly don´t have images anymore)

  • Modify the avoidancePriority. You can set the enemie´s value of 30 or 40 for example.
  • Add a NavMeshObstacle component to the "enemy" prefab
  • Use this script for the Enemy Movement

Script

using UnityEngine;
using System.Collections;
public class enemyMovement : MonoBehaviour {
    public Transform player;
    NavMeshAgent agent;
    NavMeshObstacle obstacle;

    void Start () {
        agent = GetComponent< NavMeshAgent >();
        obstacle = GetComponent< NavMeshObstacle >();
    }
    void Update () {
        agent.destination = player.position;
        // Test if the distance between the agent and the player
        // is less than the attack range (or the stoppingDistance parameter)
        if ((player.position - transform.position).sqrMagnitude < Mathf.Pow(agent.stoppingDistance, 2)) {
            // If the agent is in attack range, become an obstacle and
            // disable the NavMeshAgent component
            obstacle.enabled = true;
            agent.enabled = false;
        } else {
            // If we are not in range, become an agent again
            obstacle.enabled = false;
            agent.enabled = true;
        }
    }
}

Basically the issue that this approach tries to solve is when the player is surrounded by enemies, those which are in range to attack (almost touching the player) stop moving to attack, but the enemies in the second or third row are still trying to reach the player to kill him. Since they continue moving, they push the other enemies and the result is not really cool.

So with this script, when an enemy is in range to attack the character, it becomes an obstacle, so other enemies try to avoid them and instead of keep pushing, go around looking for another path to reach the player.

Hope it can help you some how

Ignacio Alorre
  • 7,307
  • 8
  • 57
  • 94
  • What about wen your controlling 2 units so wen you give a point they push each other i want to stop the push so they dont push each other – Zid Lyx Oct 06 '17 at 06:07
  • I think i can do the same thing wen 1 of the agents reached the destination it turns off the navmesh agent and turns on the obstical so the other agent that still didnt reach the destination goes around without pushing him – Zid Lyx Oct 06 '17 at 06:16
  • Yeah actually your second commet is mostly the idea of the answer, convert static agents (which become statics once they don´t need to keep chasing the player) in obstacles for the other agents. However I fear agents will continue crashing with each other when chasing the player, and I am not sure if enable/disable the obstacle feature of an agent ofently will impact in the performance of your game. I have to admit it is an interesting question what you have asked, but I will need to research more to come with the ultimate answer. I will keep you updated – Ignacio Alorre Oct 06 '17 at 08:02
  • can i ask 1 more question how can i check with a code if the navmesh agent is on the destination i want it to be ? – Zid Lyx Oct 06 '17 at 11:04
  • i did what i wanned and now this is how the code looks https://gist.github.com/anonymous/83cb9cfc7c63c3152fa96fd97a628a05 and i have a problem now wen i try to active the navmesh Agent wen i click with my mouse it doest work that much – Zid Lyx Oct 06 '17 at 13:04
  • Congrats for the progress, but what do you mean it doest work that much?? what are the problems you are facing? It doest work at all? – Ignacio Alorre Oct 06 '17 at 13:39
  • wen i hit the mouse button and if its far away from the agent/s it will not move if im close it will move and wen in place i will get a ton of error because agents is off GetRemainingDistance" can only be called on an active agent that has been placed on a NavMesh. UnityEngine.AI.NavMeshAgent:get_remainingDistance() – Zid Lyx Oct 06 '17 at 13:44
  • i added all the raycast stuff in to the update without it being a void so its faster it fixes all – Zid Lyx Oct 06 '17 at 13:50
  • the only thing that will bother me is that i will get errors while in game and im asking myself if there is a way not to get does because they may slow the game – Zid Lyx Oct 06 '17 at 13:52