unity机器人视野监测及寻路路径

以下为robot部分代码,其中具有robot检测player的功能,比较路径长度与检测距离问题。注释,,

void Start()
        {
            seedir = 110f;//机器人视野角度数
            alertposition =Vector3.zero;
            animplayer = GameObject.FindGameObjectWithTag("Player").GetComponent<Animator>();
            navMeshAgent = this.GetComponent<NavMeshAgent>();
            sphere = this.GetComponent<SphereCollider>();
            isinsight = false;
            otherinsightposition = Gamecontrol.getinstance.lastPlayerposition;//使用otherinsightposition来检测Gamecontrol中的lastPlayerposition是否产生变化。
            playerhealth = GameObject.FindGameObjectWithTag("Player").GetComponent<Playerhealth>();
        }
    void Update()
        {
            if(Gamecontrol.getinstance.lastPlayerposition!=otherinsightposition)//如果产生变化。
            {
                alertposition = Gamecontrol.getinstance.lastPlayerposition;//更换警报位置,更新每个机器人的追踪地点。
                otherinsightposition= Gamecontrol.getinstance.lastPlayerposition;//重新更新otherinsightposition方便检测下一次Gamecontrol中的lastPlayerposition是否产生变化。            }
        }
    public void OnTriggerStay(Collider other)
        {
            if (other.tag == "Player")
            {
                Vector3 forward = transform.forward;
                Vector3 playerdir = other.transform.position - transform.position;
                float dircha = Vector3.Angle(forward, playerdir);
                RaycastHit hitinfo;
                Physics.Raycast(this.transform.position+Vector3.up,other.transform.position-this.transform.position,out hitinfo);
                 if (dircha < 0.5f * seedir&&hitinfo.collider.tag=="Player"&&playerhealth.isdead==false)//在视野内需要保证接触到机器人trigger区域,射线畅通,人物在机器人seedir角度视野内,人物未死亡。
                {
                    isinsight = true;
                    alertposition = other.transform.position;
                    Gamecontrol.getinstance.SeePlayer(alertposition);//发送警报位置给游戏控制中心,再转达给其他机器人。
                }
                else
                {
                    isinsight = false;
                }
                 if (animplayer.GetCurrentAnimatorStateInfo(0).IsName("locostation"))//判断人物是否处于非静运动状态。
                {
                    NavMeshPath path = new NavMeshPath(); //path
                    if(navMeshAgent.CalculatePath(other.transform.position,path))//检测路径,并赋值给path。
                    {
                        Vector3[] points = new Vector3[path.corners.Length + 2];
                        points[0] = transform.position;
                        points[points.Length - 1] = other.transform.position;
                        for(int i=1; i<points.Length-1;i++)
                        {
                            points[i] = path.corners[i - 1];
                        }//以上为创建路径拐点数组。
                        float length = 0;
                        for(int i=0;i<points.Length-1;i++)
                        {
                            length += (points[i + 1] - points[i]).magnitude;//求路径长度,看路径长度与检测范围关系。
                        }
                        if(length<sphere.radius)
                        {
                            alertposition = other.transform.position;//如果小于,更新警报位置,但不更新Gamecontrol中的lastPlayerposition,意为听到,而非看到。不通知其他机器人。
                        }
                    }
                }
            }
    }
    
    
public void OnTriggerExit(Collider other)
    {
                if(other.tag=="Player")
                {
                    isinsight = false;
                }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值