AI行为

来源:互联网 发布:淘宝系统供应商 编辑:程序博客网 时间:2024/04/30 06:11

下面的这个是雷达函数,用来检测周围是不是有队友存在

using UnityEngine;using System.Collections;using System.Collections.Generic;public class Radar : MonoBehaviour {    private Collider[] colliders;    private float timer = 0;    public List<GameObject> neighbors;    public float checkInterval = 0.3f;    public float detectRadius;    public LayerMask layersChecked ;    void Start () {        neighbors = new List<GameObject>();    }    void Update () {        timer += Time.deltaTime;        //ticked        if (timer > checkInterval)        {            neighbors.Clear();            //检测一定范围内的碰撞体            colliders = Physics.OverlapSphere(transform.position, detectRadius, layersChecked);            for (int i=0; i < colliders.Length; i++)            {                    neighbors.Add(colliders[i].gameObject);            }            timer = 0;        }    }}

队友检测存在了,好啦,那么开始进行排开 围圈战斗,而不是堆积在一起

using UnityEngine;using System.Collections;[RequireComponent(typeof(Radar))]public class SteeringForSeparation : Steering {    public float comfortDistance = 1;    public float multiplierInsideComfortDistance = 2;    private Transform mTran;    void Start ()    {        mTran = this.transform;    }    void Update()    {        mTran.position += Force();    }    public override Vector3 Force()    {        Vector3 steeringForce = new Vector3(0,0,0);        foreach (GameObject s in GetComponent<Radar>().neighbors)        {            if ((s!=null)&&(s != this.gameObject))            {                Vector3 toNeighbor = transform.position - s.transform.position;                float length = toNeighbor.magnitude;                steeringForce += toNeighbor.normalized / 20;                if (length < comfortDistance)                {                    steeringForce *= multiplierInsideComfortDistance;                }                steeringForce.y = 0;                }        }        return steeringForce;    }}

以后再更新排队,聚集等团体AI行动,弄得AI就跟星际和DOTA似得

0 0