Unity 3D之简单鱼群算法(直接套上就好)

本文介绍了如何在Unity 3D中应用鱼群算法,特别提及其在AR、VR和捕鱼达人游戏中的应用。通过简单粗暴的实现方式,该算法为多个项目提供了支持,并欢迎有经验的大佬提出改进意见。
摘要由CSDN通过智能技术生成

鱼群算法在捕鱼达人类似的游戏中是不可或缺的,还有很多项目中都要用到,我本人觉得这条算法帮助我做过诸多的项目,其中有AR,VR,捕鱼达人秀等,简单粗暴,如果有需要改正的地方还请大佬们指正:

public class TankGroup : MonoBehaviour {
   

         private static List tankGroups;//所有组

        

         public LayerMask mask;//成员层

         public int groupID=0;//组id

         public float keepDistance=10, keepWeight=1;//成员保持距离和保持距离权重

         public float targetCloseDistance=20,targetWeight=1.25f, moveWeight=0.8f;//距离目标距离,距离目标权重和成员移动权重

}

坦克成员
 

 

public class TankBehaviour : MonoBehaviour {
   

         private const float minMoveCheck=0.2f;

        

         public int groupId=0;//组 id

         public float moveSpeed=5, rotateSpeed=20;//移动旋转速度

        

         public Vector3 position{
   

                   get{
   return transform.position;}

         }

        

         public Vector3 movement{
   

                   get{
   
要在Unity3D中使用集群算法模拟鱼群运动,可以按照以下步骤编写代码: 1. 创建一个鱼的预制体(Prefab),包含鱼的模型和相应的脚本组件。 2. 在鱼的脚本中,添加以下代码: ```csharp using UnityEngine; public class Fish : MonoBehaviour { public Transform target; // 集群中心点 public float cohesionWeight = 1f; // 聚集权重 public float alignmentWeight = 1f; // 对齐权重 public float separationWeight = 1f; // 分离权重 public float maxSpeed = 2f; // 最大速度 public float maxForce = 0.1f; // 最大力 private Rigidbody rb; private void Start() { rb = GetComponent<Rigidbody>(); } private void Update() { Vector3 cohesionForce = Vector3.zero; Vector3 alignmentForce = Vector3.zero; Vector3 separationForce = Vector3.zero; int cohesionCount = 0; int alignmentCount = 0; int separationCount = 0; foreach (Collider collider in Physics.OverlapSphere(transform.position, 5f)) // 选择附近的鱼 { if (collider.gameObject != gameObject) { Fish otherFish = collider.gameObject.GetComponent<Fish>(); if (otherFish != null) { cohesionForce += otherFish.transform.position; cohesionCount++; alignmentForce += otherFish.rb.velocity; alignmentCount++; if (Vector3.Distance(transform.position, otherFish.transform.position) < 1f) // 如果太近,远离 { separationForce += transform.position - otherFish.transform.position; separationCount++; } } } } if (cohesionCount > 0) { cohesionForce /= cohesionCount; cohesionForce = (cohesionForce - transform.position).normalized; } if (alignmentCount > 0) { alignmentForce /= alignmentCount; alignmentForce = alignmentForce.normalized; } if (separationCount > 0) { separationForce /= separationCount; separationForce = separationForce.normalized; } Vector3 totalForce = cohesionForce * cohesionWeight + alignmentForce * alignmentWeight + separationForce * separationWeight; Vector3 clampedForce = Vector3.ClampMagnitude(totalForce, maxForce); rb.AddForce(clampedForce); // 限制速度 rb.velocity = Vector3.ClampMagnitude(rb.velocity, maxSpeed); } } ``` 3. 在场景中实例化多只鱼预制体,并将它们添加到场景中。 4. 调整鱼的脚本中的各种参数,如聚集权重、对齐权重、分离权重、最大速度和最大力等,以获得期望的集群运动效果。 5. 运行游戏,观察鱼的集群运动效果。 这段代码实现了鱼使用集群算法进行群体运动。在Update方法中,鱼会计算与其他鱼之间的距离,并根据距离和设定的参数计算聚集、对齐和分离的力,然后将这些力施加到鱼的刚体上,使其产生运动。你可以根据自己的需求调整参数以获得期望的集群运动效果。希望对你有所帮助!如果有更多问题,请继续提问。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值