我在写追踪敌人的时候,写一个机器人追踪的时候没有问题,但是想要复制多几个机器人的时候,出现了问题。在检测到有敌人时,应该是所有机器人都要追踪才对,但是并没有,只有本体追踪了。请问这是什么原因呢?代码如下
//这个是机器人代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class aiscript : MonoBehaviour
{
public static bool FindEnemy;//发现敌人为真
public float detectfield;//探测范围
private Monitor1 monitor1 ;//监控
bool SystemdetectResult;
int length;
public float aispeed;
public float[] Distance;
public float attackfield;//public的值不需要初始化,在unity界面直接设置即可
//Vector3 DisiredmovementVector;
//Vector3 CurrentmovementVector;
public Transform playertransform,aitransform;/*,eyestransform1,eyestransform2*/
Rigidbody rigid;
Animator anim;
private NavMeshAgent ai;
private void OnDrawGizmos()//本是private
{
Gizmos.color = Color.green;
Gizmos.DrawWireSphere(aitransform.position, detectfield);
if (Vector3.Distance(playertransform.position, aitransform.position) <= detectfield)
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(aitransform.position, detectfield);
}
}
public Vector3 TrackEnemy()
{
var collider = Physics.OverlapSphere(aitransform.position, detectfield);
Vector3 EnemyPosition = new Vector3();
Vector3 EnemyPositionLog = new Vector3();
if (SystemdetectResult)
{
EnemyPositionLog = playertransform.position;
}
else
EnemyPositionLog = transform.position;
foreach (var enemy in collider)
{
if (enemy.CompareTag("Player"))
{
EnemyPosition = playertransform.position;
}
else
EnemyPosition = EnemyPositionLog;
}
return EnemyPosition;
}
void Start()
{
SystemdetectResult = false;
rigid = GetComponent();
anim = GetComponent();
//bool iswall=Iswall();
ai = GetComponent();
}
void Update()
{
/* FindEnemy = SystemDetectresult();*///应将后面函数中用到此函数的全部替换为该变量,减少系统运算负担!
AiDetectresult();
Vector3 currentposition = TrackEnemy();
SystemdetectResult = SystemDetectresult();
//Debug.Log(FindEnemy);
AiAction(currentposition);
}
public bool AiDetectresult()//ai检测到敌人
{
if (Vector3.Distance(playertransform.position, aitransform.position) <= detectfield)
return true;
else
{
Monitor1.SystemAlarm = true;
return false;
}
}
public bool SystemDetectresult()
{
if (Monitor1.Alarm || AiDetectresult())
{
return true;
}
else return false;
}public void AiAction(Vector3 Currentposition)
{
float DistanceToPhysics = Vector3.Distance(playertransform.position, transform.position);
if (SystemdetectResult)
{
Debug.Log("有敌人");
ai.SetDestination(Currentposition);
if (DistanceToPhysics < attackfield)
ai.isStopped = true;
else
ai.isStopped = false;
anim.SetFloat("forward", ai.velocity.magnitude);
}
else
{
anim.SetFloat("forward", ai.velocity.magnitude);
}
}
//以下是监控代码,挂在空物体上
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Monitor1 : MonoBehaviour
{
// Start is called before the first frame update
public float detectfield;
public Transform playertransform;
public static bool Alarm;
public static bool SystemAlarm;
public static Vector3 EnemyPosition;
void Start()
{
}
public void OnDrawGizmos()
{
Gizmos.color = Color.green;
Gizmos.DrawWireSphere(transform.position, detectfield);
if (IsAlarm())
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, detectfield);
}
}
// Update is called once per frame
void Update()
{
Alarm = IsAlarm();
}
public bool IsAlarm()
{
if (Vector3.Distance(transform.position, playertransform.position) < detectfield)
return true;
else return false;
}
}