unity 简单AI巡逻
导航基础部分请看:https://blog.csdn.net/Brave_boy666/article/details/1187058621、场景蓝色的小点是空物体 ,用来标记巡逻的点,在游戏中是看不到的烘焙我就不讲了,不懂得去看我上面的链接2、实现EnemyAIusing System.Collections;using System.Collections.Generic;using Unit
·
导航基础部分请看:https://blog.csdn.net/Brave_boy666/article/details/118705862
1、AI巡逻
1、场景
蓝色的小点是空物体 ,用来标记巡逻的点,在游戏中是看不到的
烘焙我就不讲了,不懂得去看我上面的链接
2、实现
EnemyAI
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class EnemyAI : MonoBehaviour
{
NavMeshAgent _agent;
public float speed;
public Transform[] wayPoints;
Vector3 curWayPoint;
void Start()
{
_agent = GetComponent<NavMeshAgent>();
_agent.speed = speed;
//在场景中的三个点随机选一个点前往
curWayPoint = wayPoints[Random.Range(0,wayPoints.Length)].position;
//给目标位置赋值
_agent.SetDestination(curWayPoint);
}
void Update()
{
print(_agent.radius);
//到达目的地进行位置的切换
if (_agent.remainingDistance==0)//remainingDistance指的是当前位置到目的地的水平直线距离,这个为0表示我们到达目的地
{
print(1);
//重复上面的操作
curWayPoint = wayPoints[Random.Range(0, wayPoints.Length)].position;
_agent.SetDestination(curWayPoint);
}
}
}
Point
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Point : MonoBehaviour
{
//编写编辑器辅助对象
private void OnDrawGizmos()
{
Gizmos.color = Color.blue;
Gizmos.DrawSphere(transform.position,0.5f);//绘制半径为0.5的球
}
}
3、效果
2、AI听觉
在EnemyAI脚本中添加辅助范围
public float audRange;//AI听觉范围
//只有当选择这个物体的时候才显示这个辅助对象
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position,audRange);//位置和半径
}
通过调整audRange的值来调整AI听觉的范围
更多推荐
已为社区贡献1条内容
所有评论(0)