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 的账号与群组 管理员的工作中,相当重要的一环就是『管理账号』啦!因为整个系统都是你在管理的, 并且所有一般用户的账号申请,都必须要透过你的协助才行!所以你就必须要了解一下如何管理好一个服务 ...
随机推荐
- 音频 m4a 转 wav
背景 最近做智能家居,需要用到一些应答词 需要自己录制.但是在mac下面通过 QuickTime 录制的是 m4a格式.但是应答词需要 wav格式.所以就需要转化了 解决方法 # sox 不行, ff ...
- Android字符串,颜色,尺寸资源的使用
字符串.颜色.尺寸资源文件这三种文件位于res文件夹的values文件夹中,名称分别为strings.xml , colors.xml , dimens.xml下面是例子,首先来看字符串资源文件str ...
- 二、springboot使用jpa
花了几天时间,好好看了看springboot的jpa部分,总结了常用的形式. 1.通过STS工具添加jpa的依赖项 要连mysql,测试的时候需要web,顺便添加了lombok不写set和get方法了 ...
- ES6 Symbol数据类型和set-map 数据结构
Symbol数据类型 ES6新加的数据类型,提供一个独一无二的值 { let a1 = Symbol() ;let a2 = Symbol() } //声明 { let a3 = Symbol.for ...
- 冒泡排序(JAVA实现)
基本思想:在要排序的一组数中,对当前还未排好序的范围内的全部数,自上而下对相邻的两个数依次进行比较和调整,让较大的数往下沉,较小的往上冒. 即:每当两相邻的数比较后发现它们的排序与排序要求相反时,就将 ...
- webservice学习教程(三)--
快速入门 首先,我们来尝试一下调用别人写好的webService 来体验一把:我们访问http://www.webxml.com.cn/zh_cn/index.aspx 进入到里边 当我们输入一个号码 ...
- http类中的download方法 下载汉字文件名 汉字消失的问题
将文件名用urlencode转码即可 $http = new \Org\Net\Http; $http->download($fileName, urlencode($showName));
- [LeetCode] 70. Climbing Stairs_ Easy tag: Dynamic Programming
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- P3803 【模板】多项式乘法(FFT)
传送门: 参考博客 1:大佬 attack 参考博客 2:大佬 胡小兔 在这里再膜拜一下这两位大佬 Orz%%% #include<iostream> #include<cstd ...
- Solaris环境下使用snoop命令抓包
(1)报文抓取 Solaris中自带有snoop抓包工具,通过执行相应的命令抓取. 抓取目的地址为10.8.3.250的数据包,并存放到/opt/cap250的文件里 snoop -o /opt/ca ...