最近在在学习AI寻路中,找到了一些比较实用的干货,就是用Physic.RayCast 让敌人AI绕过障碍物找到玩家。
看到了别人的总结,所以分享一下,Physic.RayCast是一种能解决AI绕过障碍物的方法,但并不是一个完美的解决方法,同时里面一些参数需要根据游戏场景稍微进行调整。代码来源转自:http://www.theappguruz.com/blog/unity-3d-enemy-obstacle-awarness-ai-code-sample
代码:
Enemy AI
using UnityEngine;
using System.Collections;
public class EnemyAI: MonoBehaviour {
// Fix a range how early u want your enemy detect the obstacle.
private int range;
private float speed;
private bool isThereAnyThing = false;
// Specify the target for the enemy.
public GameObject target;
private float rotationSpeed;
private RaycastHit hit;
// Use this for initialization
void Start() {
range = 80;
speed = 10 f;
rotationSpeed = 15 f;
}
// Update is called once per frame
void Update() {
//Look At Somthly Towards