en造数据结构与算法C# 群组行为优化 和 头鸟控制

实现:

1.给鸟类随机播放随机动画使得每一只鸟扇翅膀的频率都不尽相同

2.可以自行添加权重,并在最后 sumForce = separationForce + cohesionForce + alignmentForce;分别乘上相应权重,这样鸟就能快速飞行和转向辣

复制代码
using System.Collections.Generic;
using UnityEngine;
using System.Collections;

public class BoidsCode1 : MonoBehaviour {
    Rigidbody rb;
    //鸟群存储线性表
    public List<GameObject> birdNeighbors = new List<GameObject>();

    //separation分离  alignment对齐 cohesion凝聚
    public Vector3 separationForce = Vector3.zero;
    public Vector3 alignmentForce = Vector3.zero;
    public Vector3 cohesionForce = Vector3.zero;
    //平均位置,鸟群朝向
    public Vector3 averagePosition = Vector3.zero;
    public Vector3 birdForward = Vector3.zero;
    //鸟群速度
    public Vector3 birdVelocity = Vector3.zero;
    //总力
    public Vector3 sumForce;

    //检测间隔
    public float checkInterval = 0.2f;
    //检测距离
    public float checkDistance = 2;
    //分离力最大值
    public float maxSeparationForce = 1.0f;

    private void Awake() {
        rb = GetComponent<Rigidbody>();
    }

    private void Start() {
        InvokeRepeating("CalcForce", 0, checkInterval);
    }

    //计算函数
    private void CalcForce() {
        //清空邻居鸟的列表
        birdNeighbors.Clear();
        //检测到范围内所有的鸟
        Collider[] colliders = Physics.OverlapSphere(transform.position, checkDistance);
        foreach (Collider collider in colliders) {
            if (collider != null && collider.gameObject != this.gameObject) {
                //添加到列表里面
                birdNeighbors.Add(collider.gameObject);
            }
        }

        //平均点位置为0
        averagePosition = Vector3.zero;

        //朝向设置为0
        birdForward = Vector3.zero;
        birdVelocity = Vector3.zero;

        separationForce = Vector3.zero; //分离力重设
        cohesionForce = Vector3.zero; //凝聚力重设
        alignmentForce = Vector3.zero; //对齐力重设

        foreach (GameObject bird in birdNeighbors) {
            //设定分离力的方向
            Vector3 spForceDirection = (this.transform.position - bird.transform.position);
            if (spForceDirection.magnitude > 0) {
                separationForce += spForceDirection.normalized / spForceDirection.sqrMagnitude;
            }
            //得到鸟群位置(加起来的和)
            averagePosition += bird.transform.position;
            //得到鸟群的方向和速度
            birdForward += bird.transform.forward;
            birdVelocity += bird.GetComponent<Rigidbody>().velocity;
        }

        //限制分离力的最大值
        if (separationForce.magnitude > maxSeparationForce) {
            separationForce = separationForce.normalized * maxSeparationForce;
        }

        //计算平均位置
        if (birdNeighbors.Count > 0) {
            averagePosition /= birdNeighbors.Count;
        }

        //设定凝聚力的方向
        Vector3 cohesionDirection = (averagePosition - transform.position).normalized;
        if (cohesionDirection.magnitude > 0) {
            cohesionForce += cohesionDirection;
        }

        //求取平均速度
        if (birdNeighbors.Count > 0) {
            alignmentForce = birdVelocity / birdNeighbors.Count;
        }

        施加分离力
        //rb.AddForce(separationForce, ForceMode.VelocityChange);
        施加凝聚力
        //rb.AddForce(cohesionForce, ForceMode.VelocityChange);
        施加对齐力
        //rb.AddForce(alignmentForce, ForceMode.VelocityChange);
        sumForce = separationForce + cohesionForce + alignmentForce;
     
        rb.AddForce(sumForce,ForceMode.Force);
        // 仅当当前鸟不是头鸟时,才设置朝向
        if (birdForward.magnitude > 0 && !gameObject.CompareTag("Leader")) {
            this.transform.forward = birdForward.normalized;
        }
        给予一只鸟速度平均速度
        //if (alignmentForce.magnitude > 0) {
        //    this.GetComponent<Rigidbody>().velocity = alignmentForce;
        //}
    }
}
相关推荐
hsjkdhs17 分钟前
C++之类的继承与派生
开发语言·c++
lly20240640 分钟前
HTML 元素:构建网页的基础
开发语言
低调小一41 分钟前
LRU缓存科普与实现(Kotlin 与 Swift)
开发语言·缓存·kotlin
爱好学习的青年人41 分钟前
一文详解Go语言字符串
开发语言·后端·golang
浅川.251 小时前
xtuoj string
开发语言·c++·算法
咕白m6251 小时前
C# 合并多个PDF文档:高效解决方案
c#·.net
望获linux1 小时前
【实时Linux实战系列】实时系统的可观测性:Prometheus 与 Grafana 集成
大数据·linux·服务器·开发语言·网络·操作系统
加油吧zkf1 小时前
Python入门:从零开始的完整学习指南
开发语言·前端·python
xqlily2 小时前
Kotlin:现代编程语言的革新者
android·开发语言·kotlin
csbysj20203 小时前
XSLT Apply:深入解析XSLT在XML转换中的应用
开发语言