鉄無癮 2022-03-01 15:39 采纳率: 100%
浏览 63
已结题

Unity复制体与本体的逻辑关系

我在写追踪敌人的时候,写一个机器人追踪的时候没有问题,但是想要复制多几个机器人的时候,出现了问题。在检测到有敌人时,应该是所有机器人都要追踪才对,但是并没有,只有本体追踪了。请问这是什么原因呢?代码如下

//这个是机器人代码
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;
}

}

  • 写回答

1条回答 默认 最新

  • 陈言必行 Unity领域优质创作者 2022-03-02 09:24
    关注

    这个代码格式看起来实在是有点费劲,根据你的描述,我推测是因为复制出来的共有变量赋值问题,比如下图这两个变量,建议检查一下赋值是否正确:

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 3月10日
  • 已采纳回答 3月2日
  • 创建了问题 3月1日

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀