群体行为之分离
可参考外文博客:点击打开链接
如图,实现时,为了计算分离行为所需要的操纵力,首先要搜索指定领域内的其他邻居,然后对每个邻居,计算AI角色到该邻居的向量r,将r归一化,得到排斥力的方向,然后排斥力的大小与距离成反比,然后把来自邻居的排斥力相加,就得到了分离行为的总操控力。
检测附近的邻居,雷达装置
using UnityEngine;
using System.Collections;
using System.Collections.Generic;public class Radar : MonoBehaviour {private Collider[] colliders;//碰撞体数组private float timer = 0;//计时器public List neighbors;//邻居列表public float checkInterval = 0.3f;//检测邻居间隔public float detectRadius = 2f;//检测半径public LayerMask layersChecked;//检测哪一层的游戏对象// Use this for initializationvoid Start () {neighbors = new List();}// Update is called once per framevoid Update () {timer += Time.deltaTime;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;public class SteeringForSeparation : MonoBehaviour
{public Vector3 velocity = Vector3.zero;public float mass = 50f;//质量public float comfortDistance = 2;public float multipierInsideComfortDistance = 1;private Vector3 seperationForce;private Vector3 CohesionForce;private Vector3 AlignmentForce;private Vector3 SumForce;float length = 0;// private Rigidbody rg;// Use this for initializationvoid Start () {// rg = GetComponent();}// Update is called once per framevoid Update () {Force();CaluForce();}public void Force(){seperationForce = Vector3.zero;AlignmentForce = Vector3.zero;SumForce = Vector3.zero;foreach (GameObject s in GetComponent().neighbors){if((s!=null)&s!=this.gameObject){Vector3 toNeighbor = transform.position - s.transform.position; //与邻居的距离length = toNeighbor.magnitude;seperationForce += toNeighbor.normalized / length;if(length().neighbors.Count);if ((GetComponent().neighbors.Count>1)){Vector3 a = SumForce / mass; //牛二率velocity += a*Time.deltaTime;//Debug.Log (velocity);//rg.AddForce(velocity, ForceMode.Force);transform.Translate(velocity * Time.deltaTime, Space.World);}}
}
运行前:
运行后:
可以看到达到分离效果
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
