Fork me on GitHub

unity_小功能实现(敌人追踪主角)

 

1、敌人发现主角有两种形式:

  a、看见主角(主角出现在敌人的视野之内)

  b、听见主角(听见主角走路声或者是跑步声)

a:看(see)

首先判断主角是否在敌人视野角度内,那么我们只需要判断B<0.5*A能不能成立

 

 

b:听(listen)

 

 

 代码如下:

using UnityEngine;
using System.Collections;
using UnityEngine.AI;

public class EnemySight : MonoBehaviour {

    private float seeAngle=120;//敌人视野角度
    private bool isSeePlay = false;

    private Vector3 lastPos;// 玩家的最后位置
    private Vector3 alermPos=Vector3.zero; //警报位置   

    private Animator anim;  //主角动画,作用是判断主角是否在运动
    private SphereCollidersphereCollider;//敌人身上的碰撞器,该碰撞器是用来触发检测主角是否在敌人可见,可听范围内
    private NavMeshAgentnavMeshAgent; //AI组件  

    void Awake()
    {
        anim = GameObject.FindGameObjectWithTag(Tags.player).GetComponent<Animator>();
        lastPos = GameController._instance.lastPlayerPostion;
        navMeshAgent = GetComponent<NavMeshAgent>();
    }

    void Update()
    {
        //同步主角位置
        if (lastPos != GameController._instance.lastPlayerPostion)//触发警报后玩家位置改变
        {
            alermPos = GameController._instance.lastPlayerPostion; //更新警报位置
            lastPos = GameController._instance.lastPlayerPostion;
        }
        
    }

    void OnTriggleStay(Collider other)
    {
        if (other.tag==Tags.player)
        {
            //看玩家
            Vector3 startDir = transform.forward;//敌人开始朝向
            Vector3 currDir = other.transform.position - transform.position; //敌人看向玩家的向量
            float angle = Vector3.Angle(startDir, currDir);//敌人开始朝向与看见玩家朝向的夹角
            if (angle <seeAngle * 0.5f) //视野角度一半以内可见
            {
                //主角在敌人的视野之内
                isSeePlay = true;
                alermPos = other.transform.position;//把玩家的位置设置为警报位置
                GameController._instance.SeePlayer(other.transform);
            }
            else
            {
                isSeePlay = false;
            }


            //听脚步声音
            if (anim.GetCurrentAnimatorStateInfo(0).IsName("Locomotion"))//如果玩家在运动
            {
                NavMeshPath path = new NavMeshPath();
                if (navMeshAgent.CalculatePath(other.transform.position, path))
                {
                    Vector3[] wayPoints = new Vector3[path.corners.Length+2];
                    wayPoints[0] = transform.position;
                    wayPoints[wayPoints.Length - 1] = other.transform.position;
                    for (inti = 0; i<path.corners.Length; i++)
                    {
                        wayPoints[i + 1] = path.corners[i];  
                    }
                    float length = 0;
                    for (inti = 1; i<wayPoints.Length; i++)
                    {
                        length += (wayPoints[i] - wayPoints[i - 1]).magnitude; //所有节点连接的折线的总长度
                    }
                    if (length <= sphereCollider.radius) //在听力范围内
                    {
                        alermPos = other.transform.position;
                    }
                }
            }

        }
    }

    void OnTriggleExit(Collider other)
    {
        if (other.tag == Tags.player)
        {
            isSeePlay = false;
        }
    }

 

posted @ 2018-05-29 16:08  爱上游戏开发  阅读(2794)  评论(0编辑  收藏  举报
 >>>转载请注明出处