using System.Collections.Generic;
using UnityEngine; public class CrowAI : MonoBehaviour {
//当前速度
public float speed = ;
public Vector3 velocity = Vector3.forward;
private Vector3 startVelocity;//初始化当前速度
public Transform target;//目标点
//总力
public Vector3 sumForce = Vector3.zero;
//质量
public float m = ; //分离的力
public float sparationDistance = ; //分离的距离
public List<GameObject> sparationNeighbors = new List<GameObject>();
public float sparationWeight = ; //权重
public Vector3 sparationForce = Vector3.zero; //队列的力
public float alignmentDistance = ;
public List<GameObject> alignmentNeighbors = new List<GameObject>();
public float alignmentWeight = ; //权重
public Vector3 alignmentForce = Vector3.zero; //聚集的力
public float cohesionWeight = ;//权重
public Vector3 cohesionForce = Vector3.zero;
//间隔几秒检测一次
public float checkInterval = 0.2f; private Animation anim;
public float animRandom = ;
void Start()
{
target = GameObject.Find("target").transform;
startVelocity = velocity;
//0 秒后调用 CalcForce() 方法,并且之后每隔 checkInterval 秒调用一次 CalcForce() 方法
InvokeRepeating("CalcForce", , checkInterval);
anim = GetComponentInChildren<Animation>();
Invoke("Playanim", Random.Range(, animRandom));
}
void Playanim() {
anim.Play();
} void CalcForce() {
//计算之前先归零
sumForce = Vector3.zero;
sparationForce = Vector3.zero;
alignmentForce = Vector3.zero;
cohesionForce = Vector3.zero; //先清空集合
sparationNeighbors.Clear();
Collider[] colliders = Physics.OverlapSphere(transform.position, sparationDistance);
foreach (Collider c in colliders)
{
//判断该物体近的邻居
if (c!=null && c.gameObject!=this.gameObject)
{
sparationNeighbors.Add(c.gameObject);
}
} //1.计算分离的力
foreach (GameObject neighbor in sparationNeighbors)
{
Vector3 dir = transform.position - neighbor.transform.position;
sparationForce += dir.normalized / dir.magnitude;
}
//如果附近有物体
if (sparationNeighbors.Count > )
{
//得到分离的力
sparationForce *= sparationWeight;
//得到的力赋值给总力
sumForce += sparationForce;
} //2.计算队列的力(整体一个前进的力)
alignmentNeighbors.Clear();
colliders = Physics.OverlapSphere(transform.position, alignmentDistance);
foreach (Collider c in colliders)
{
if (c!=null &&c.gameObject !=this.gameObject)
{
alignmentNeighbors.Add(c.gameObject);
}
}
//计算邻居的平均朝向
Vector3 avgDir = Vector3.zero;
//朝向的总和
foreach (GameObject n in alignmentNeighbors)
{
avgDir += n.transform.forward;
}
if (alignmentNeighbors.Count >)
{
//得到平均数
avgDir /= alignmentNeighbors.Count;
//得到相对方向
alignmentForce = avgDir = transform.forward;
alignmentForce *= alignmentWeight;
//得到的力赋值给总力
sumForce += alignmentForce;
} //3.聚集的力
if (alignmentNeighbors.Count > )
{
Vector3 center = Vector3.zero;
foreach (GameObject n in alignmentNeighbors)
{
center += n.transform.position;
}
center /= alignmentNeighbors.Count;
Vector3 dirToCenter = center - transform.position;
cohesionForce += dirToCenter.normalized * velocity.magnitude;
cohesionForce *= cohesionWeight;
sumForce += cohesionForce;
} //4.保持恒定飞行速度的力
Vector3 enginForce = velocity.normalized * startVelocity.magnitude;
sumForce += enginForce * 0.1f;
//4.保持恒定目标飞行的效果
Vector3 targetDir = target.position - transform.position;
sumForce += (targetDir.normalized - transform.forward)*speed;
} void Update () {
//加速度(根据牛顿第二定律)
Vector3 a = sumForce / m;
//计算出速度
velocity += a * Time.deltaTime;
//物体运行
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(velocity), Time.deltaTime*);
transform.Translate(transform.forward * Time.deltaTime * velocity .magnitude, Space.World);
}
}

AI_群组行为的更多相关文章

  1. 微信小程序的机会在于重新理解群组与二维码

    历时一年,唯一一个尚未发布就获得Pony Ma与Allen Zhang站台的产品:微信小程序,将于2017年1月9日正式上线了.我很期待.唯一要警惕的是:防止长考出臭棋. 在上线前夕,我对于如何借助小 ...

  2. 一行实现QQ群组头像,微信群组,圆角等效果. 并支持url直接加载图片

    说点题外话. Coding中我们总是经历着这么几个过程. 学会使用: 不管是API也好, 开源库也好. 总是在最开始的学会去用. 了解实现原理: 可能会因为一些不兼容, 代码的异常状态的处理不够完美等 ...

  3. 添加群组表AppGroup和群组详细表AppGroupDetails

    添加群组表AppGroup和群组详细表AppGroupDetails CREATE TABLE [dbo].[AppGroup]( ) NOT NULL PRIMARY KEY, ) NOT NULL ...

  4. linux基础-第七单元 用户、群组及权限的深入讨论

    怎样查看文件的权限 ls命令详解 root用户及文件的安全控制 什么是root用户 用户分类 文件操作权限 文件操作权限的字符表示 chown chgrp 使用符号表示法设定文件或目录上的权限 chm ...

  5. linux基础-第六单元 用户、群组和权限

    用户及passwd文件 /etc/passwd文件的功能 /etc/passwd文件每个字段的具体含义 shadow文件 /etc/shadow文件的功能 /etc/shadow文件每个字段的具体含义 ...

  6. linux命令之 用户和群组

    一.保存用户信息的文件 1 /etc/passwd root:x:::root:/root:/bin/bash pwftp:x::::/alidata/www/wwwroot/:/sbin/nolog ...

  7. Linux系统之用户、群组和权限

    一.用户管理 创建用户时,系统为用户分配一个唯一的编号UID,同时为用户创建一个同名的组,并为组分配一个编号GID,并把该用户加入该组中. 系统规定: uid: 0       特权用户      u ...

  8. #Linux学习笔记# Linux文件的所有者、群组和其他人

    1.关于所有者.群组和其他人 在Linux系统中,每个文件都具有User.Group和Others三种身份的权限配置.那这三种身份分别表示什么意思呢?配置这三种身份的权限有啥意义呢? (1)文件所有者 ...

  9. Linux 的账号与群组[转自vbird]

    Linux 的账号与群组 管理员的工作中,相当重要的一环就是『管理账号』啦!因为整个系统都是你在管理的, 并且所有一般用户的账号申请,都必须要透过你的协助才行!所以你就必须要了解一下如何管理好一个服务 ...

随机推荐

  1. 理解 Memory barrier(内存屏障)无锁环形队列

    原文:https://www.cnblogs.com/my_life/articles/5220172.html Memory barrier 简介 程序在运行时内存实际的访问顺序和程序代码编写的访问 ...

  2. webpack摸索笔记

    上一个链接,入门webpack看这篇文章最好:https://segmentfault.com/a/1190000006178770 1.先安装好node 2.建个项目文件 3,.window+r,打 ...

  3. 意外的php之学习笔记

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/gc_gongchao/article/details/37312039     什么是php? ph ...

  4. if(a)

    let a = undefined; let b = null; let c = ''; let d = ; let e = {}; let f = []; if (a) { console.log( ...

  5. php优秀的库

    PHP 是一种通用开源脚本语言.语法吸收了 C 语言.Java 和 Perl 的特点,利于学习,使用广泛,主要适用于 Web 开发领域,是大多数后端开发者的首选.PHP 作为最受欢迎的编程语言之一,经 ...

  6. MongoDB系列----uupdate和数组

    db.collection.update( criteria, objNew, upsert, multi ) criteria : update的查询条件,类似sql update查询内where后 ...

  7. Kinetis Design Studio 下使用J-Link下载程序

    1.安装J-Link驱动. 在KDS安装目录下已自带J-Link驱动,进入以下目录(以我的为例): C:\Freescale\KDS_1.1.1\segger\USBDriver 将看到CDC.x64 ...

  8. spider随机请求头和ip

    #创建爬虫 scrapy genspider randomIp_spider "taobao.com" #把需要请求的url放到一个混淆的url请求list中去,避免被监测到总是访 ...

  9. java生成二维码工具类

    package com.runtime.extend.utils.CodeCreate; import com.google.zxing.*;import com.google.zxing.commo ...

  10. 将文件转成clob添加到Oracle数据库中

    Controller层: @Controller@RequestMapping(value = {"/Test/TestController"})public class Test ...