微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Unity中的群组行为

一:效果演示

不使用群组算法和使用群组算法的演示视频



二:什么是群组行为

群组行为属于人工智能思想,比如说人群走路、鸟群飞行、鱼群游动,群组中成员之间不应该很规律的运动,例如不能拥挤到一起,而是更加真实的去模拟现实中的场景
下面是不使用群组行为和使用群组行为的演示

群组中的三种行为:
——分隔:成员间保持一定距离,不能太近
——队列:群组以相同的速度向相同方向运动
——聚集:避免与临近成员距离太远


三:群组行为中的三种行为

为了更好的模拟现实环境,使用施加力的方式替代直接修改物体速度,因为直接修改物体的速度会无视各种外力,而施加力是一种仿物理的方式

——分隔

separation diagram

当成员之间相距很近时,给其一个相反的力进行分离

 

——队列

alignment diagram

当与大部队运动方向不一致时,给其一个力进行调整

 

——聚集

cohesion diagram


当与其他成员相距很远时,就选取就近几个成员的中心点,施加力实现聚集


四:代码实现

using UnityEngine;

/// <summary>
/// 群组行为中的个体
/// </summary>
public class UnityFlock : MonoBehavIoUr
{
    public Transform target;//目标物体

    [Header("运动速度")]
    public float speed;
    [Header("物体质量")]
    private int m = 1;

    [Header("需要分离的距离")]
    [Header("====================")]
    public float separationdist;
    [Header("分离力的比重")]
    public float separationWeight;

    [Header("需要队列的距离")]
    public float alignmentdist;
    [Header("队列力的比重")]
    public float alignmentWeight;

    [Header("需要聚集的距离")]
    public float cohesiondist;
    [Header("聚集力的比重")]
    public float cohesionWeight;

    private Vector3 veLocity;//速度

    private void Awake()
    {
        veLocity = Vector3.forward;
    }

    private void Update()
    {
        Vector3 sumForce = Vector3.zero;
        sumForce += Separation() * separationWeight;
        sumForce += Alignment() * alignmentWeight;
        sumForce += Cohesion() * cohesionWeight;
        sumForce += Seek() * speed;

        Vector3 a = sumForce / m;
        veLocity += a * Time.deltaTime;

        transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(veLocity), 0.2f);
        transform.Translate(transform.forward * Time.deltaTime, Space.World);
    }

    /// <summary>
    /// 寻找目标
    /// </summary>
    /// <returns></returns>
    private Vector3 Seek()
    {
        Vector3 force = Vector3.zero;
        Vector3 dir = target.position - transform.position;
        force = dir.normalized - transform.forward;
        return force;
    }

    /// <summary>
    /// 得到分离的力
    /// </summary>
    private Vector3 Separation()
    {
        Vector3 separationForce = Vector3.zero;
        GameObject[] objs = GameObject.FindGameObjectsWithTag("UnityFlock");
        foreach (var unity in objs)
        {
            if (unity == gameObject)
            {
                continue;
            }
            if (Vector3.distance(transform.position, unity.transform.position) <= separationdist)
            {
                Vector3 dir = transform.position - unity.transform.position;
                separationForce += dir.normalized / dir.magnitude;
            }
        }
        return separationForce;
    }

    /// <summary>
    /// 得到队列的力
    /// </summary>
    private Vector3 Alignment()
    {
        Vector3 alignmentForce = Vector3.zero;
        GameObject[] objs = GameObject.FindGameObjectsWithTag("UnityFlock");
        Vector3 avgDir = Vector3.zero;
        int counter = 0;
        foreach (var unity in objs)
        {
            if (unity == gameObject)
            {
                continue;
            }
            if (Vector3.distance(transform.position, unity.transform.position) < alignmentdist)
            {
                counter++;
                avgDir += unity.transform.forward;
            }
        }
        avgDir /= counter;
        alignmentForce += (avgDir - transform.forward).normalized;
        return alignmentForce;
    }

    /// <summary>
    /// 聚集
    /// </summary>
    private Vector3 Cohesion()
    {
        Vector3 cohesionForce = Vector3.zero;
        GameObject[] objs = GameObject.FindGameObjectsWithTag("UnityFlock");
        Vector3 avgDir = Vector3.zero;
        int counter = 0;
        foreach (var unity in objs)
        {
            if (unity == gameObject)
            {
                continue;
            }
            if (Vector3.distance(transform.position, unity.transform.position) < alignmentdist)
            {
                counter++;
                avgDir += unity.transform.position;
            }
        }
        avgDir /= counter;
        cohesionForce += (avgDir - transform.position).normalized;
        return cohesionForce;
    }
}

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐