我在做一个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();
}
}
}
}
}发布于 2017-10-05 21:01:48
从下面的链接 (遗憾的是,它不再有图像)
脚本
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;
}
}
}基本上,这个方法试图解决的问题是当玩家被敌人包围,那些在攻击范围内的玩家(几乎触碰到玩家)停止移动攻击,但是第二排或第三排的敌人仍然试图接近玩家去杀死他。因为他们继续前进,他们推其他敌人,结果并不是真正的酷。
所以有了这个剧本,当敌人在攻击角色时,它就变成了一个障碍,所以其他的敌人试图避开他们,而不是不停地推,四处寻找另一条到达玩家的道路。
希望能对你有所帮助
https://stackoverflow.com/questions/46592616
复制相似问题