Unity Navmesh 代理

Unity navmesh agents

我正在开发一款 RTS 游戏。我做了一个正常的移动脚本,我增加了代理的停止距离,这样他们就不会互相看着对方并抽那么多。但他们还是打了起来。

我找不到让代理互相避开而不互相推动的方法。或者以某种方式忽略物理,同时仍然试图避免彼此。这是点击移动的代码

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();
                }
            }
        }
    }
}

来自以下link(可惜没有图片了)

  • 修改avoidancePriority。您可以设置敌人的价值 例如 30 或 40。
  • 将 NavMeshObstacle 组件添加到 "enemy" 预制件
  • 将此脚本用于敌人移动

脚本

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;
        }
    }
}

基本上这个方法试图解决的问题是当玩家被敌人包围时,那些在攻击范围内(几乎接触到玩家)的敌人停止移动攻击,但第二排或第三排的敌人仍在试图接近玩家以杀死他。由于他们继续移动,他们推了其他敌人,结果不是很酷。

所以在这个脚本中,当一个敌人在攻击角色的范围内时,它就变成了一个障碍,所以其他敌人试图避开他们而不是继续推动,而是四处寻找另一条路径来接近玩家。

希望对你有所帮助