AI攻击与巡逻

public GameObject player;
void Update () {
        Vector3 playerPos = player.transform.position;
        float distance = Vector3.Distance(transform.position, playerPos);
//距离小于2米并且在60度攻击范围内
float angle = Vector3.Angle(transform.forward.normalized,(target.position - transform.position).normalized);
        if (distance <= 2f && angle<30)
        {
 			//停止寻路  
             agent.ResetPath();
transform.LookAt(player.transform.position);   // 转身看着玩家
            //攻击玩家
            Debug.Log("攻击玩家");
        }
        else if (distance <= 5f)
        {
            //追踪玩家
            Debug.Log("追踪玩家");
f = true;
            nav.destination = player.transform.position;
        }
        else
        {
            //回到出生地,继续巡逻
            Debug.Log("继续巡逻");
            Patrol();
        }
	}

    void Patrol()//巡逻
    {
            timer += Time.deltaTime;
            if (timer >= waitTimer)
            {
                index++;
                index %= 4;
                timer = 0;
                Debug.Log(index);
                nav.destination = waypoints[index].position;
            }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值