AI_群组行为
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_群组行为的更多相关文章
- 微信小程序的机会在于重新理解群组与二维码
历时一年,唯一一个尚未发布就获得Pony Ma与Allen Zhang站台的产品:微信小程序,将于2017年1月9日正式上线了.我很期待.唯一要警惕的是:防止长考出臭棋. 在上线前夕,我对于如何借助小 ...
- 一行实现QQ群组头像,微信群组,圆角等效果. 并支持url直接加载图片
说点题外话. Coding中我们总是经历着这么几个过程. 学会使用: 不管是API也好, 开源库也好. 总是在最开始的学会去用. 了解实现原理: 可能会因为一些不兼容, 代码的异常状态的处理不够完美等 ...
- 添加群组表AppGroup和群组详细表AppGroupDetails
添加群组表AppGroup和群组详细表AppGroupDetails CREATE TABLE [dbo].[AppGroup]( ) NOT NULL PRIMARY KEY, ) NOT NULL ...
- linux基础-第七单元 用户、群组及权限的深入讨论
怎样查看文件的权限 ls命令详解 root用户及文件的安全控制 什么是root用户 用户分类 文件操作权限 文件操作权限的字符表示 chown chgrp 使用符号表示法设定文件或目录上的权限 chm ...
- linux基础-第六单元 用户、群组和权限
用户及passwd文件 /etc/passwd文件的功能 /etc/passwd文件每个字段的具体含义 shadow文件 /etc/shadow文件的功能 /etc/shadow文件每个字段的具体含义 ...
- linux命令之 用户和群组
一.保存用户信息的文件 1 /etc/passwd root:x:::root:/root:/bin/bash pwftp:x::::/alidata/www/wwwroot/:/sbin/nolog ...
- Linux系统之用户、群组和权限
一.用户管理 创建用户时,系统为用户分配一个唯一的编号UID,同时为用户创建一个同名的组,并为组分配一个编号GID,并把该用户加入该组中. 系统规定: uid: 0 特权用户 u ...
- #Linux学习笔记# Linux文件的所有者、群组和其他人
1.关于所有者.群组和其他人 在Linux系统中,每个文件都具有User.Group和Others三种身份的权限配置.那这三种身份分别表示什么意思呢?配置这三种身份的权限有啥意义呢? (1)文件所有者 ...
- Linux 的账号与群组[转自vbird]
Linux 的账号与群组 管理员的工作中,相当重要的一环就是『管理账号』啦!因为整个系统都是你在管理的, 并且所有一般用户的账号申请,都必须要透过你的协助才行!所以你就必须要了解一下如何管理好一个服务 ...
随机推荐
- Autofac之实例获取
通过之前的学习了解了使用Autofac第一步是通过向容器中注册类型,然后从容器中获取类型实例该怎么做呢? 本次主要学习如何在Autofac中获取注册类型实例 Resolve 使用Resolve方法获取 ...
- Ext选项卡tabpanel切换动态加载数据
鸣人不说暗话,来张图: 代码开始:(使用Ext,ajax加载数据,如果你们有好的方法也可以多多交流)var tabxsk = new Object(); //初始化 tabxsk.init = fun ...
- Typora极简教程
Typora极简教程 ” Markdown 是一种轻量级标记语言,创始人是约翰·格鲁伯(John Gruber).它允许人们 “使用易读易写的纯文本格式编写文档,然后转换成有效的 HTML 文档.” ...
- C#开启异步 线程的四种方式
一.异步委托开启线程public static void Main(string[] args){ Action<int,int> a=add; a.BeginInvoke(3,4,nul ...
- java基础语法(三大基础)
一.标识符 标识符是用于包名.类名.变量名.方法名.对象名.数组名.集合名等的命名: 规则: (1)可以使用英文字母.阿拉伯数字.下划线_.$符号 (2)不能以数字开头 (3)不能使用Java中的 ...
- 面向对象:MATLAB的自定义类 [MATLAB]
https://www.cnblogs.com/gentle-min-601/p/9785812.html 面向对象:MATLAB的自定义类 [MATLAB] 这几天刚刚开始学习MATLAB的面向 ...
- CDI services--Decorators(装饰器)
1.Decorators装饰器综述 拦截器是一种强大的方法在应用程序捕捉运行方法和解耦.拦截器可以拦截任何java类型的调用. 这使得拦截器适合解决事务管理,安全性,以及日记记录. 本质上说,拦截器并 ...
- JDK8 HashMap--treeify()树形化方法
/*创建红黑树*/ final void treeify(Node<K,V>[] tab) { TreeNode<K,V> root = null;// 定义红黑树根节点roo ...
- mongodb和python交互
一.安装pymongo包 sudo pip install pymongo 二.新增数据: 增加一条: from pymongo import MongoClient client = MongoCl ...
- Vue2.0 入门 安装Vue-cli
因为要用到npm命令先按装node.js 具体安装如下: 一.安装Node.js步骤 1.下载对应你系统的Node.js版本:https://nodejs.org/en/download/2.选安装目 ...