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 的账号与群组 管理员的工作中,相当重要的一环就是『管理账号』啦!因为整个系统都是你在管理的, 并且所有一般用户的账号申请,都必须要透过你的协助才行!所以你就必须要了解一下如何管理好一个服务 ...
随机推荐
- 软件开发项目组各职能介绍 & 测试人员在团队中的定位
前言 接触了许多非测试和新入行的测试从业者,听到最多的问题就是:“测试是否被需要?“ 团队职能介绍 <暗黑者1>中有句台词,“专案组有五个职能角色构成,侦探.网警.痕迹 ...
- 工程经验记录yolo框下移引发的思考
事件叙述: 缘起:yolo检测框,在项目开始之前由于在一些场景下观察到yolo的框在大车检测的时候,回归的位置不好,一些车辆的尾部会被漏掉,特别是车牌部分,恰好被框在外面的时候,在后续就无法进行车牌检 ...
- 如何查看正在执行sql的语句及其父语句调用?如何查看正在执行SQL的具体参数值与执行计划?
---SQL Server查询正在执行的SQL语句及执行计划 select ds.session_id,dr.start_time,db_name(dr.database_id),dr.blockin ...
- 如何优雅的运行起jmeter
安装jdk 下载网站:http://www.oracle.com/,选个最新的版本就可以. 注意:11以后没有自动生成jre 配置环境变量 对于Java程序开发而言,主要会使用JDK的两个命令:jav ...
- Django---框架简介和工程搭建
Django框架 一.Django介绍 二.Django工程搭建 回到顶部 一.Django介绍 1.简介 Django的主要目的是简便.快速的开发数据库驱动的网站.它强调代码复用,多个组件可以 ...
- man scp
SCP(1) BSD General Commands Manual SCP(1) NAME scp - secure copy (remote file copy program) ...
- LigerUi之ligerMenu 右键菜单
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- sql相同表不同查询条件合并显示
关键字:FULL JOIN 只要其中某个表存在匹配,FULL JOIN 关键字就会返回行. select a.createtime, ISNULL(lp, 0) lp , ISNULL(hp, 0) ...
- 取模性质,快速幂,快速乘,gcd和最小公倍数
一.取模运算 取模(取余)运算法则: 1. (a+b)%p=(a%p+b%p)%p; 2.(a-b)%p=(a%p-b%p)%p; 3.(a*b)%p=(a%p * b%p)%p; 4.(a^b)%p ...
- SQL Server创建存储过程——动态SQL
简介: 存储过程(stored procedure)是一组为了完成特定功能的SQL语句集合,经编译后存储在服务器端的数据库中,利用存储过程可以加速SQL语句的执行. 自定义存储过程,由用户创建并能完成 ...