首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >单位肚脐剂

单位肚脐剂
EN

Stack Overflow用户
提问于 2017-10-05 18:37:04
回答 1查看 1.7K关注 0票数 1

我在做一个RTS游戏。我做了一个正常的移动脚本,我增加了代理的停止距离,这样他们就不会互相看了看,然后抽那么多水。但他们还是互相攻击。

我找不到办法让探员们互相回避而不是互相推搡。或者以某种方式无视物理学,同时又试图回避对方。以下是单击要移动的代码

代码语言:javascript
复制
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();
                }
            }
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-05 21:01:48

从下面的链接 (遗憾的是,它不再有图像)

  • 修改avoidancePriority。例如,您可以将enemie的值设置为30或40。
  • 在“敌人”预制件中添加一个NavMeshObstacle组件
  • 使用此脚本进行敌人运动

脚本

代码语言:javascript
复制
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;
        }
    }
}

基本上,这个方法试图解决的问题是当玩家被敌人包围,那些在攻击范围内的玩家(几乎触碰到玩家)停止移动攻击,但是第二排或第三排的敌人仍然试图接近玩家去杀死他。因为他们继续前进,他们推其他敌人,结果并不是真正的酷。

所以有了这个剧本,当敌人在攻击角色时,它就变成了一个障碍,所以其他的敌人试图避开他们,而不是不停地推,四处寻找另一条到达玩家的道路。

希望能对你有所帮助

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46592616

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档