当前位置: 首页 > news >正文

怎么做付费网站建设银行官网入口

怎么做付费网站,建设银行官网入口,怎么做网站免费的刷赞,小程序定制开发团队大家好#xff0c;我是七七#xff0c;今天来给大家介绍的是Unity中用操控行为实现的跟随领队行为。 看本文若是想了解和实现#xff0c;只看本文即可#xff0c;若是想彻底弄透#xff0c;建议从七七的游戏AI专栏开始看。 废话不多说#xff0c;先上视频#xff1a; …  大家好我是七七今天来给大家介绍的是Unity中用操控行为实现的跟随领队行为。 看本文若是想了解和实现只看本文即可若是想彻底弄透建议从七七的游戏AI专栏开始看。 废话不多说先上视频 跟随队长 我们的目标是让后面的人跟着领头的人并遵循一些规则 对于领头的人 随机地移动检测前方是否有人 对于跟随的人 跟的不要太近人与人直接不要拥挤如果发现挡道领头人路了赶紧让开  我们只需要实现这些规则就可以得到理想的效果这与神经网络的思想类似下面我们就来实现这些规则。 规则1 这个脚本是挂载领头人身上的目的是在前方LEADER_BEHIND_DIST处画一个圆球若是说前方有人则让这些人避开。 using System.Collections; using System.Collections.Generic; using UnityEngine;public class DrawGizmos : MonoBehaviour {public float evadeDistance;public Vector3 center;private Vehicle vehicleScript;private float LEADER_BEHIND_DIST;void Start(){vehicleScript GetComponentVehicle();LEADER_BEHIND_DIST 2.0f;}void Update(){center transform.position vehicleScript.velocity.normalized * LEADER_BEHIND_DIST;}void OnDrawGizoms(){Gizmos.DrawWireSphere(center, evadeDistance);} }规则2 这个脚本是挂在一个空物体上的目的是生成多个跟随者 using System.Collections; using System.Collections.Generic; using UnityEngine;public class GenerateBotsForFollowLeader : MonoBehaviour {public GameObject botPrefab;public GameObject leader;public int botCount;public float minX -5f;public float maxX 5.0f;public float minZ -5.0f;public float maxZ 5.0f;public float Yvalue 1.026003f;void Start(){Vector3 spawnPosition;GameObject bot;for(int i 0; i botCount; i){spawnPosition new Vector3(Random.Range(minX, maxX), Yvalue, Random.Range(minZ, maxZ));//随机产生一个生成位置bot Instantiate(botPrefab, spawnPosition, Quaternion.identity) as GameObject;bot.GetComponentSteeringForLeaderFollowing().leader leader;bot.GetComponentSteeringForEvade().target leader;bot.GetComponentSteeringForEvade().enabled false;bot.GetComponentEvadeController().leader leader;}} }规则3 这个脚本是挂在跟随者身上的是为了确定跟随者的路径目标点即领导人身后LEADER_BEHIND_DIST处 using System.Collections; using System.Collections.Generic; using UnityEngine; [RequireComponent(typeof(SteeringForArrive))] public class SteeringForLeaderFollowing : Steering {public Vector3 target;private Vector3 desiredVelocity;//预期速度private Vehicle m_vehicle;//获得被操控的AI角色private float maxSpeed;private bool isPlanar;public GameObject leader;private Vehicle leaderController;private Vector3 leaderVelocity;private float LEADER_BEHIND_DIST2.0f;private SteeringForArrive arriveScript;private Vector3 randomOffset;void Start(){m_vehicle GetComponentVehicle();maxSpeed m_vehicle.maxSpeed;isPlanar m_vehicle.isPlanar;leaderControllerleader.GetComponentVehicle();arriveScript GetComponentSteeringForArrive();//为抵达行为指定目标点arriveScript.target new GameObject(arriveTarget);arriveScript.target.transform.position leader.transform.position;}public override Vector3 Force(){leaderVelocity leaderController.velocity;targetleader.transform.positionLEADER_BEHIND_DIST*(-leaderVelocity).normalized;//计算目标点arriveScript.target.transform.position target;return new Vector3(0, 0, 0);} }规则4 这个脚本是挂在跟随者身上的目的是为了避免跟随者挡住领导人的路 using System.Collections; using System.Collections.Generic; using UnityEngine;public class EvadeController : MonoBehaviour {public GameObject leader;private Vehicle leaderLocomotion;private Vehicle m_vehicle;private bool isPlanar;private Vector3 leaderAhead;private float LEADER_BEHIND_DIST;private Vector3 dist;public float evadeDistance;private float sqrEvadeDistance;private SteeringForEvade evadeScript;void Start(){leaderLocomotion leader.GetComponentVehicle();evadeScript GetComponentSteeringForEvade();m_vehicle GetComponentVehicle();isPlanarm_vehicle.isPlanar;LEADER_BEHIND_DIST 2.0f;sqrEvadeDistancesqrEvadeDistance*sqrEvadeDistance;}void Update(){leaderAheadleader.transform.positionleaderLocomotion.velocity.normalized*LEADER_BEHIND_DIST; //计算领队前方的一个点dist transform.position - leaderAhead;if (isPlanar){dist.y 0;}if(dist.sqrMagnitude sqrEvadeDistance){evadeScript.enabled true;Debug.DrawLine(transform.position, leader.transform.position);}else{evadeScript.enabled false;}} }实现 前提 有3个基类 UnityAI——操控行为编程的主要基类-CSDN博客 第一步 创建一个场景一个Plane 第二步 创建一个Cube作为领队起名为Leader为其挂上三个脚本如下所示 using System.Collections; using System.Collections.Generic; using UnityEngine;public class AILocomotion : Vehicle {private CharacterController controller; //AIĽɫprivate Rigidbody theRigidbody;private Vector3 moveDistance;//AIɫÿεƶvoid Start(){controller GetComponentCharacterController();theRigidbody GetComponentRigidbody();moveDistance new Vector3(0, 0, 0);base.Start();//ûstartijʼ}//زFixedUpdateиvoid FixedUpdate(){velocity acceleration * Time.fixedDeltaTime;//ٶif (velocity.sqrMagnitude sqrMaxSpeed) //ٶvelocity velocity.normalized * maxSpeed;moveDistance velocity * Time.fixedDeltaTime;if (isPlanar) {velocity.y 0;moveDistance.y 0;}if (controller ! null)//ѾΪAIɫӽɫôýɫʹƶcontroller.SimpleMove(velocity);//ɫûɫҲûRigidbody//RigidbodyҪɶѧķʽƶelse if (theRigidbody null || !theRigidbody.isKinematic)transform.position moveDistance;else //Rigidbodyƽɫ˶theRigidbody.MovePosition(theRigidbody.positionmoveDistance);if(velocity.sqrMagnitude0.00001)//³ٶȴһֵΪ˷ֹ{Vector3 newForward Vector3.Slerp(transform.forward, velocity, damping * Time.deltaTime);if(isPlanar)newForward.y 0;transform.forward newForward;}//߶gameObject.GetComponentAnimation().Play(walk);} }using System.Collections; using System.Collections.Generic; using UnityEngine;public class SteeringForWander : Steering {public float wanderRadius; //徘徊半径public float wanderDistance; //徘徊距离public float wanderJitter; //每秒加到目标的随即位移的最大值public bool isPlanar;private Vector3 desiredVelocity;//预期速度private Vehicle m_vehicle;//获得被操控的AI角色private float maxSpeed;private Vector3 circleTarget;private Vector3 wanderTarget;void Start(){m_vehicle GetComponentVehicle();maxSpeed m_vehicle.maxSpeed;isPlanar m_vehicle.isPlanar;circleTarget new Vector3(wanderRadius * 0.707f, 0, wanderRadius * 0.707f); //选取与安全上的一个点作为初始点}public override Vector3 Force(){Vector3 randomDisplacement new Vector3((Random.value - 0.5f) * 2 * wanderJitter, (Random.value - 0.5f) * 2 * wanderJitter, (Random.value - 0.5f) * 2 * wanderJitter);if (isPlanar)randomDisplacement.y 0;circleTargetrandomDisplacement;//将随机位移加到初始点上circleTarget wanderRadius * circleTarget.normalized;//由于新位置很可能不在圆周上因此需要投影到圆周上wanderTarget m_vehicle.velocity.normalized * wanderDistance circleTarget transform.position;//之前计算出的值是相对于AI的需要转换为世界坐标desiredVelocity (wanderTarget - transform.position).normalized * maxSpeed;return (desiredVelocity - m_vehicle.velocity);} }using System.Collections; using System.Collections.Generic; using UnityEngine;public class DrawGizmos : MonoBehaviour {public float evadeDistance;public Vector3 center;private Vehicle vehicleScript;private float LEADER_BEHIND_DIST;void Start(){vehicleScript GetComponentVehicle();LEADER_BEHIND_DIST 2.0f;}void Update(){center transform.position vehicleScript.velocity.normalized * LEADER_BEHIND_DIST;}void OnDrawGizoms(){Gizmos.DrawWireSphere(center, evadeDistance);} }第三步 创建一个空物体起名为follersGenerator用于生成跟随者并添加脚本 using System.Collections; using System.Collections.Generic; using UnityEngine;public class GenerateBotsForFollowLeader : MonoBehaviour {public GameObject botPrefab;public GameObject leader;public int botCount;public float minX -5f;public float maxX 5.0f;public float minZ -5.0f;public float maxZ 5.0f;public float Yvalue 1.026003f;void Start(){Vector3 spawnPosition;GameObject bot;for(int i 0; i botCount; i){spawnPosition new Vector3(Random.Range(minX, maxX), Yvalue, Random.Range(minZ, maxZ));//随机产生一个生成位置bot Instantiate(botPrefab, spawnPosition, Quaternion.identity) as GameObject;bot.GetComponentSteeringForLeaderFollowing().leader leader;bot.GetComponentSteeringForEvade().target leader;bot.GetComponentSteeringForEvade().enabled false;bot.GetComponentEvadeController().leader leader;}} }第四步 创建一个方块预设作为跟随者挂上下列脚本 using System.Collections; using System.Collections.Generic; using UnityEngine;public class AILocomotion : Vehicle {private CharacterController controller; //AIĽɫprivate Rigidbody theRigidbody;private Vector3 moveDistance;//AIɫÿεƶvoid Start(){controller GetComponentCharacterController();theRigidbody GetComponentRigidbody();moveDistance new Vector3(0, 0, 0);base.Start();//ûstartijʼ}//زFixedUpdateиvoid FixedUpdate(){velocity acceleration * Time.fixedDeltaTime;//ٶif (velocity.sqrMagnitude sqrMaxSpeed) //ٶvelocity velocity.normalized * maxSpeed;moveDistance velocity * Time.fixedDeltaTime;if (isPlanar) {velocity.y 0;moveDistance.y 0;}if (controller ! null)//ѾΪAIɫӽɫôýɫʹƶcontroller.SimpleMove(velocity);//ɫûɫҲûRigidbody//RigidbodyҪɶѧķʽƶelse if (theRigidbody null || !theRigidbody.isKinematic)transform.position moveDistance;else //Rigidbodyƽɫ˶theRigidbody.MovePosition(theRigidbody.positionmoveDistance);if(velocity.sqrMagnitude0.00001)//³ٶȴһֵΪ˷ֹ{Vector3 newForward Vector3.Slerp(transform.forward, velocity, damping * Time.deltaTime);if(isPlanar)newForward.y 0;transform.forward newForward;}//߶gameObject.GetComponentAnimation().Play(walk);} }using System.Collections; using System.Collections.Generic; using UnityEngine;public class SteeringForArrive : Steering {public bool isPlanar true;public float arrivalDistance 0.3f;public float characterRadius 1.2f;public float slowDownDistance;public GameObject target;private Vector3 desiredVelocity;//预期速度private Vehicle m_vehicle;//获得被操控的AI角色private float maxSpeed;void Start(){m_vehicle GetComponentVehicle();maxSpeed m_vehicle.maxSpeed;isPlanar m_vehicle.isPlanar;}public override Vector3 Force(){Vector3 toTarget target.transform.position - transform.position;Vector3 desiredVelocity;Vector3 returnForce;if (isPlanar)toTarget.y 0;float distance toTarget.magnitude;if (distance slowDownDistance){desiredVelocity toTarget.normalized * maxSpeed;returnForce desiredVelocity - m_vehicle.velocity;}else{desiredVelocity toTarget - m_vehicle.velocity;returnForce desiredVelocity - m_vehicle.velocity;}return returnForce;}void OnDrawGizmos(){Gizmos.DrawWireSphere(target.transform.position, slowDownDistance);} } using System.Collections; using System.Collections.Generic; using UnityEngine; [RequireComponent(typeof(SteeringForArrive))] public class SteeringForLeaderFollowing : Steering {public Vector3 target;private Vector3 desiredVelocity;//预期速度private Vehicle m_vehicle;//获得被操控的AI角色private float maxSpeed;private bool isPlanar;public GameObject leader;private Vehicle leaderController;private Vector3 leaderVelocity;private float LEADER_BEHIND_DIST2.0f;private SteeringForArrive arriveScript;private Vector3 randomOffset;void Start(){m_vehicle GetComponentVehicle();maxSpeed m_vehicle.maxSpeed;isPlanar m_vehicle.isPlanar;leaderControllerleader.GetComponentVehicle();arriveScript GetComponentSteeringForArrive();//为抵达行为指定目标点arriveScript.target new GameObject(arriveTarget);arriveScript.target.transform.position leader.transform.position;}public override Vector3 Force(){leaderVelocity leaderController.velocity;targetleader.transform.positionLEADER_BEHIND_DIST*(-leaderVelocity).normalized;//计算目标点arriveScript.target.transform.position target;return new Vector3(0, 0, 0);} }using System.Collections; using System.Collections.Generic; using UnityEngine;public class Radar : MonoBehaviour {private Collider[] colliders;//碰撞体的组数private float timer 0;//计时器public ListGameObject neighbors;public float checkInterval 0.3f;//设置检测的时间间隔public float detectRadius 10f;//设置邻域半径public LayerMask layersChecked;//设置检测哪一层的游戏对象void Start(){neighbors new ListGameObject();}void Update(){timer Time.deltaTime;if(timer checkInterval){neighbors.Clear();colliders Physics.OverlapSphere(transform.position, detectRadius, layersChecked);//查找当前AI角色邻域内的所有碰撞体for(int i 0; i colliders.Length; i)//对于每个检测到的碰撞体获取Vehicle组件并且加入邻居列表钟{if (colliders[i].GetComponentVehicle())neighbors.Add(colliders[i].gameObject);}timer 0;}} }using System.Collections; using System.Collections.Generic; using UnityEngine;public class SteeringForSeparation : Steering {public float comforDistance 1;//可接受的距离public float multiplierInsideComfortDistance 2;//当AI角色与邻居距离过近时的惩罚因子public override Vector3 Force(){Vector3 steeringForce new Vector3(0, 0, 0);foreach(GameObject s in GetComponentRadar().neighbors)//遍历这个AI角色的邻居列表中的每个邻居{if ((s ! null) (s ! this.gameObject)){Vector3 toNeighbor transform.position - s.transform.position;//计算当前AI角色与邻居s之间的距离float lengthtoNeighbor.magnitude;steeringForce toNeighbor.normalized / length;//计算这个邻居引起的操控力if (length comforDistance)steeringForce * multiplierInsideComfortDistance;}}return steeringForce;} }using System.Collections; using System.Collections.Generic; using UnityEngine;public class SteeringForEvade :Steering {public GameObject target;private Vector3 desiredVelocity;//预期速度private Vehicle m_vehicle;//获得被操控的AI角色private float maxSpeed;void Start(){m_vehicle GetComponentVehicle();maxSpeed m_vehicle.maxSpeed;}public override Vector3 Force(){Vector3 toTarget target.transform.position - transform.position;float lookaheadTime toTarget.magnitude / (maxSpeed target.GetComponentVehicle().velocity.magnitude);//向前预测的时间desiredVelocity (transform.position - (target.transform.positiontarget.GetComponentVehicle().velocity*lookaheadTime)).normalized * maxSpeed;return (desiredVelocity - m_vehicle.velocity);} }using System.Collections; using System.Collections.Generic; using UnityEngine;public class EvadeController : MonoBehaviour {public GameObject leader;private Vehicle leaderLocomotion;private Vehicle m_vehicle;private bool isPlanar;private Vector3 leaderAhead;private float LEADER_BEHIND_DIST;private Vector3 dist;public float evadeDistance;private float sqrEvadeDistance;private SteeringForEvade evadeScript;void Start(){leaderLocomotion leader.GetComponentVehicle();evadeScript GetComponentSteeringForEvade();m_vehicle GetComponentVehicle();isPlanarm_vehicle.isPlanar;LEADER_BEHIND_DIST 2.0f;sqrEvadeDistancesqrEvadeDistance*sqrEvadeDistance;}void Update(){leaderAheadleader.transform.positionleaderLocomotion.velocity.normalized*LEADER_BEHIND_DIST; //计算领队前方的一个点dist transform.position - leaderAhead;if (isPlanar){dist.y 0;}if(dist.sqrMagnitude sqrEvadeDistance){evadeScript.enabled true;Debug.DrawLine(transform.position, leader.transform.position);}else{evadeScript.enabled false;}} }收尾 最后再给各个角色装上刚体设置好Leader等参数就可以了。 很多行为我们都可以通过设定规则来实现可能乍一看行为很难摸索但慢慢分析出其中的规则并逐一实现后问题往往就会被解决
http://www.pierceye.com/news/485176/

相关文章:

  • 眉山市规划建设局网站专做网页的网站
  • 珠海网站建设开发ck网站
  • 医疗网站设计小程序开发制作费用
  • 德州网站建设网页设计实验报告总结
  • 易烊千玺个人网站入口什么是网站建设的建议
  • 哪个网站做供求信息app开发公司排行榜
  • 信誉好的广州外贸网站未来做哪些网站能致富
  • 运城推广型网站建设温州的网站建设公司
  • 怎么样做网站编程一般通过哪些行为来处理人际关系
  • 学校的网站开发过程wordpress公司展示网站
  • 贵港市建设局网站网站建设优化之优化关键字
  • 网站开发设计比较好的公司电子烟网站设计
  • 群辉 wordpress套件阜阳网站优化
  • 如何做网站哪个站推广网站自助建设平台
  • 西安大网站建设公司排名沈阳网络维护公司
  • 个人建立一个网站要多少钱乔拓云h5制作
  • 蒙阴网站建设百度指数排名
  • 视频网站如何推广做模具做什么网站
  • 关于旅游的网站建设论文广州外贸网站建设公司价格
  • 怎么给自己制作一个网站wordpress 中文摘要
  • 如何看网站的ftp服装网站建设策划书3000字
  • 无锡网站建设 网站制作常见的网站首页布局有哪几种
  • 网站研发PHP MYSQL网站开发全程实
  • 简约型网站国外做电商平台的网站还有什么
  • 云南昆明网站建设公司jsp网站开发详解下载
  • 上海h5网站开发网站建设在开封找谁做
  • 滨海建设局官方网站营销网络平台
  • 中国小康建设网是骗子网站吗?建设宁波市分行的互联网网站
  • 制造网站建设自己做游戏资讯网站
  • 网站建设质量如何衡量都江堰网站开发