Unity中实现群组行为
生活随笔
收集整理的這篇文章主要介紹了
Unity中实现群组行为
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一:效果演示
不使用群組算法和使用群組算法的演示視頻
二:什么是群組行為
群組行為屬于人工智能思想,比如說人群走路、鳥群飛行、魚群游動,群組中成員之間不應(yīng)該很規(guī)律的運動,例如不能擁擠到一起,而是更加真實的去模擬現(xiàn)實中的場景
下面是不使用群組行為和使用群組行為的演示
群組中的三種行為:
——分隔:成員間保持一定距離,不能太近
——隊列:群組以相同的速度向相同方向運動
——聚集:避免與臨近成員距離太遠
三:群組行為中的三種行為
為了更好的模擬現(xiàn)實環(huán)境,使用施加力的方式替代直接修改物體速度,因為直接修改物體的速度會無視各種外力,而施加力是一種仿物理的方式
——分隔
當(dāng)成員之間相距很近時,給其一個相反的力進行分離
——隊列
當(dāng)與大部隊運動方向不一致時,給其一個力進行調(diào)整
——聚集
當(dāng)與其他成員相距很遠時,就選取就近幾個成員的中心點,施加力實現(xiàn)聚集
四:代碼實現(xiàn)
using UnityEngine;/// <summary> /// 群組行為中的個體 /// </summary> public class UnityFlock : MonoBehaviour {public Transform target;//目標(biāo)物體[Header("運動速度")]public float speed;[Header("物體質(zhì)量")]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>/// 尋找目標(biāo)/// </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;} }總結(jié)
以上是生活随笔為你收集整理的Unity中实现群组行为的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JavaFX-绘制时钟界面
- 下一篇: 陈鸥,我为自己代言