using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class crow2 : MonoBehaviour {
public Vector3 vectory = Vector3.forward; //当前物体前进方向
public Vector3 diyVectory = Vector3.zero;
public Vector3 totalF=Vector3.zero; //合力
public Vector3 separateF=Vector3.zero; //分离力
public Vector3 alignmentF = Vector3.zero; //队列力
public Vector3 gatherF = Vector3.zero; //聚集力
public float separateWeight = 1; //分离力权重
public float gatherWeight = 1f; //分离力权重
public float alignmentWeight = 1f; //分离力权重
public Animation anim;
public float m = 1;
public float checkInterval = 0.2f; //重复调用的时间
public List<GameObject> separateList = new List<GameObject>(); //
public float separateRadius=3f; //分离的半径
public List<GameObject> alignList = new List<GameObject>(); //
public float alignmentRadius = 6f; //聚集的半径
public List<GameObject>gatherList = new List<GameObject>(); //
public float gatherRadius = 6f; //聚集的半径
public Transform transfrom1; //一个目标
// Use this for initialization
void Start () {
transfrom1 = GameObject.Find("tran").transform;
//diyVectory = vectory;
InvokeRepeating("separate",0,checkInterval); //重复调用某个函数
anim = GetComponentInChildren<Animation>(); //
Invoke("playAnim",Random.Range(0,2f)); //调用某个函数
}
void playAnim() //播动画函数
{
anim.Play();
}
void separate() //自行调用函数减少资源配置
{
totalF = Vector3.zero; //合力
separateF = Vector3.zero; //分离力
alignmentF = Vector3.zero; //队列力
gatherF = Vector3.zero; //聚集力
separateList.Clear(); //先清空集合
Collider[] separateCollider = Physics.OverlapSphere(transform.position,separateRadius); //用来装collider的集合
foreach (Collider c in separateCollider) //用collider检测附近
{
if (c!=null&&c.gameObject!=this.gameObject)
{
separateList.Add(c.gameObject);
}
}
foreach (GameObject gt in separateList) //遍历附近的人集合,用自己减他人获得力向量
{
Vector3 dir = (this.transform.position - gt.transform.position);
separateF += dir.normalized / dir.magnitude; //先单位化力,再除(距离向量的长度)近力大远力小
}
if (separateList.Count>0)
{
separateF *= separateWeight; //用权重做调整
totalF += separateF; //合力添加
}
alignList.Clear(); //先清空集合
Collider[] alignCollider = Physics.OverlapSphere(transform.position, alignmentRadius); //用来装collider的集合
foreach (Collider c in alignCollider) //用collider检测附近
{
if (c != null && c.gameObject != this.gameObject)
{
alignList.Add(c.gameObject);
}
}
Vector3 dir1=Vector3.zero;
foreach (GameObject gt in alignList) //遍历附近的人集合,用自己减他人获得力向量
{
dir1 += gt.transform.forward;
}
Vector3 averageV = dir1 / alignList.Count;
alignmentF = averageV - transform.forward;
if (alignList.Count > 0)
{
alignmentF *= alignmentWeight; //用权重做调整
totalF += alignmentF; //合力添加
}
if (alignList.Count>0)
{
Vector3 postion = Vector3.zero;
foreach (GameObject gt in alignList) //遍历附近的人集合,用自己减他人获得力向量
{
postion+= gt.transform.position;
}
Vector3 postionCenter = postion / alignList.Count;
Vector3 centerF = postionCenter - transform.position;
alignmentF += centerF * alignmentWeight;
totalF += alignmentF;
}
Vector3 targetF = ((transfrom1.position - transform.position).normalized-transform.forward)*20f;
totalF += targetF; //目标方向标量化-当前方向*适当速度
//不减的话转向会很慢
}
// Update is called once per frame
void Update () {
//Vector3 engineF = (diyVectory - vectory);
//if ((vectory.z > diyVectory.magnitude * 3) || (vectory.z < diyVectory.magnitude * 0.3)) //给物体速度设置最值回弹
//{
// //totalF += engineF;
// vectory.z += engineF.z;
//}
Vector3 a = totalF / m; //定义一个加速度
vectory += a * Time.deltaTime; //加速度变成速度
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(vectory), Time.deltaTime*3);
//为保证撞向不突兀给转向设置一个时间
//transform.LookAt(transfrom1.position); //Quaternion.LookRotation(vectory)为速度方向
transform.Translate(vectory * Time.deltaTime, Space.World); //
}
using System.Collections.Generic;
using UnityEngine;
public class crow2 : MonoBehaviour {
public Vector3 vectory = Vector3.forward; //当前物体前进方向
public Vector3 diyVectory = Vector3.zero;
public Vector3 totalF=Vector3.zero; //合力
public Vector3 separateF=Vector3.zero; //分离力
public Vector3 alignmentF = Vector3.zero; //队列力
public Vector3 gatherF = Vector3.zero; //聚集力
public float separateWeight = 1; //分离力权重
public float gatherWeight = 1f; //分离力权重
public float alignmentWeight = 1f; //分离力权重
public Animation anim;
public float m = 1;
public float checkInterval = 0.2f; //重复调用的时间
public List<GameObject> separateList = new List<GameObject>(); //
public float separateRadius=3f; //分离的半径
public List<GameObject> alignList = new List<GameObject>(); //
public float alignmentRadius = 6f; //聚集的半径
public List<GameObject>gatherList = new List<GameObject>(); //
public float gatherRadius = 6f; //聚集的半径
public Transform transfrom1; //一个目标
// Use this for initialization
void Start () {
transfrom1 = GameObject.Find("tran").transform;
//diyVectory = vectory;
InvokeRepeating("separate",0,checkInterval); //重复调用某个函数
anim = GetComponentInChildren<Animation>(); //
Invoke("playAnim",Random.Range(0,2f)); //调用某个函数
}
void playAnim() //播动画函数
{
anim.Play();
}
void separate() //自行调用函数减少资源配置
{
totalF = Vector3.zero; //合力
separateF = Vector3.zero; //分离力
alignmentF = Vector3.zero; //队列力
gatherF = Vector3.zero; //聚集力
separateList.Clear(); //先清空集合
Collider[] separateCollider = Physics.OverlapSphere(transform.position,separateRadius); //用来装collider的集合
foreach (Collider c in separateCollider) //用collider检测附近
{
if (c!=null&&c.gameObject!=this.gameObject)
{
separateList.Add(c.gameObject);
}
}
foreach (GameObject gt in separateList) //遍历附近的人集合,用自己减他人获得力向量
{
Vector3 dir = (this.transform.position - gt.transform.position);
separateF += dir.normalized / dir.magnitude; //先单位化力,再除(距离向量的长度)近力大远力小
}
if (separateList.Count>0)
{
separateF *= separateWeight; //用权重做调整
totalF += separateF; //合力添加
}
alignList.Clear(); //先清空集合
Collider[] alignCollider = Physics.OverlapSphere(transform.position, alignmentRadius); //用来装collider的集合
foreach (Collider c in alignCollider) //用collider检测附近
{
if (c != null && c.gameObject != this.gameObject)
{
alignList.Add(c.gameObject);
}
}
Vector3 dir1=Vector3.zero;
foreach (GameObject gt in alignList) //遍历附近的人集合,用自己减他人获得力向量
{
dir1 += gt.transform.forward;
}
Vector3 averageV = dir1 / alignList.Count;
alignmentF = averageV - transform.forward;
if (alignList.Count > 0)
{
alignmentF *= alignmentWeight; //用权重做调整
totalF += alignmentF; //合力添加
}
if (alignList.Count>0)
{
Vector3 postion = Vector3.zero;
foreach (GameObject gt in alignList) //遍历附近的人集合,用自己减他人获得力向量
{
postion+= gt.transform.position;
}
Vector3 postionCenter = postion / alignList.Count;
Vector3 centerF = postionCenter - transform.position;
alignmentF += centerF * alignmentWeight;
totalF += alignmentF;
}
Vector3 targetF = ((transfrom1.position - transform.position).normalized-transform.forward)*20f;
totalF += targetF; //目标方向标量化-当前方向*适当速度
//不减的话转向会很慢
}
// Update is called once per frame
void Update () {
//Vector3 engineF = (diyVectory - vectory);
//if ((vectory.z > diyVectory.magnitude * 3) || (vectory.z < diyVectory.magnitude * 0.3)) //给物体速度设置最值回弹
//{
// //totalF += engineF;
// vectory.z += engineF.z;
//}
Vector3 a = totalF / m; //定义一个加速度
vectory += a * Time.deltaTime; //加速度变成速度
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(vectory), Time.deltaTime*3);
//为保证撞向不突兀给转向设置一个时间
//transform.LookAt(transfrom1.position); //Quaternion.LookRotation(vectory)为速度方向
transform.Translate(vectory * Time.deltaTime, Space.World); //
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class poChange : MonoBehaviour {
// Use this for initialization
void Start () {
InvokeRepeating("change",0,3);//给目标设置一个随机速度三秒一次(我用带有动画的鸟做动体)
}
void change()
{
float ranX = Random.Range(0, 100f);
float ranY = Random.Range(0, 100f);
float ranZ = Random.Range(0, 100f);
transform.position = new Vector3(ranX, ranY, ranZ);
}
// Update is called once per frame
void Update () {
}
}
版权声明:本文为RootTimeYu原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。