找回密码
 立即注册
查看: 9436|回复: 95

[脚本] Unity3D教程之如何使用 Enemy Aim Ai

[复制链接]
发表于 2015-11-10 16:54 | 显示全部楼层 |阅读模式
一、Enemy Aim Ai
目的: 这篇文章的主要目的是为了让您了解有关如何使用 Enemy Aim Ai 。你会得到结果:
Enemy aim AI是非常有用的,当你想要敌人一直监视player。适当争取对象在真实世界的场景,需要时间,所以敌人会采取一些之前它锁在目标系统上的时间量。
这种效应可以创建的 Lerping 旋转角度对玩家的敌人。这种情况是在动作游戏,敌人跟随,目的是何地然后射球员的情况下非常有用。对敌人遵循概念是在早些时候发布的博客中已经讨论过。在推行针对在游戏的同时,理解四元数的概念是非常必要的。
四元数存储对象的旋转,还可以计算价值取向。一个可以直接玩欧拉角,但可能会出现 万向锁 的情况。
根据当地的坐标系统,如果你旋转模型X 轴,然后其 Y 和 Z 轴经验万向节锁被”锁定”在一起。
现在回到我们的主题,给出了下面是一个简单的例子来解释 Enemy aim AI
按照下面给出的步骤。
1、创建Cube将作用在player control
应用适当的材料,根据要求向其网格渲染器
将 TargetMovementScript 应用于游戏对象
将Cube 作为对象来将移动上 player’scommands。
TargetMovementScript.cs
public class TargetMovementScript : MonoBehaviour {
public float targetSpeed=9.0f;//Speed At Which the Object Should Move
void Update ()
transform.Translate (Input.GetAxis ("Horizontal")*Time.deltaTime*targetSpeed,Input.GetAxis ("Vertical")*Time.deltaTime*targetSpeed,0);
}
}
2、创建一个Enemy对象,由箭头和Cube组成,
1、应用适当的材料对Cube 。
2、使箭头Sprite作为一个不同的对象
3、其方向由 Cube指出 . 所以在这里,箭头将充当了敌人的枪。
4、此箭头将指向目标对象和它看起来就好像它试图锁定目标
5、我们也可以操纵速度,敌人将能够锁定目标属性 ;作为不同的敌人应该是不同的困难,以及不同的功能。
6、一辆坦克应采取更多的时间锁定一名士兵用枪瞄准。所以在其中之一可以锁定目标的速度应该是不同的。
EnemyAimScript.cs
public class EnemyAimScript : MonoBehaviour {
public Transform target; // Target Object
public float enemyAimSpeed=5.0f; // Speed at Which Enenmy locks on the Target
Quaternion newRotation;
float orientTransform;
float orientTarget;
void Update () {
orientTransform = transform.position.x;
orientTarget = target.position.x;
// To Check on which side is the target , i.e. Right or Left of this Object
if (orientTransform > orientTarget) {
// Will Give Rotation angle , so that Arrow Points towards that target
newRotation = Quaternion.LookRotation (transform.position - target.position, -Vector3.up);
}
else {
newRotation = Quaternion.LookRotation (transform.position - target.position,Vector3.up);
}
// Here we have to freeze rotation along X and Y Axis, for proper movement of the arrow
newRotation.x = 0.0f;
newRotation.y = 0.0f;
// Finally rotate and aim towards the target direction using Code below
transform.rotation = Quaternion.Lerp (transform.rotation,newRotation,Time.deltaTime * enemyAimSpeed);
// Another Alternative
// transform.rotation = Quaternion.RotateTowards(transform.rotation,newRotation, Time.deltaTime * enemyAimSpeed);
}
}
7、Hierarchy 和 Scene View 可能会像作为给定下面
备注:-
可以改变敌人目标并设置锁定目标的速度。
可以脚本通过允许X 或 Y 轴旋转,为了解这一概念的 。
可以给敌人添加Follow 脚本,以便敌人Follow 和 瞄准 玩家。
而不是使用 Quaternion.Lerp 你也可以使用 Quaternion.RotateTowards 达到相同的效果。
二、通过有限状态机实现AI
Contents Objective Step –
1: Set up the scene Step –
2: Create and place Game Objects Step –
3: Implement the BoxMovement Script Step –
4: FSM Modelled Script Step –
5: AI Script for the Box –
目的: 这篇文章的主要目的是要给你一个关于如何使用有限状态机模型在 实现 AI 。
FSM 的基础知识:
在游戏中实施 AI Finite State Machine Framework是完美的, 产生伟大的结果,而无需复杂的代码。 它是一个模型,由一个或多个状态。可以在一段时间只有一个单一的状态处于活动状态, 所以这台机器必须从一个状态到另一种的转换 为执行不同的操作。
有限状态机框架通常用于管理、 组织和 代表不同的状态、执行流,在游戏中实现人工智能是非常有用的。”brain”是敌人,例如,可以使用有限状态机来实现: 每个状态表示一个动作,例如 巡逻、 Chase、 逃避或拍摄或任何其他种类的行动。
AI FSMs 的工作与Unity的的Animation FSMs,在那里一个动画状态将更改为另一个符合的要求。可以通过使用第三方插件像行为实施 AI FSM 或它可以直接由脚本以及执行。
若要获取有限状态机框架的基本理念,我们将执行一个教程最简单的方法,使用 switch 语句。然后我们将学习如何使用一个框架,使 AI 实现易于管理和扩展。
请按照下面的步骤来执行简单的有限状态机。
在这里我们将会有两个Box,在那里将由玩家控制一个Box和 对方将由 AI 控制Box。此 AI 控制的Box会在chasing追逐状态或patrolling巡逻状态,即Box中将开始追逐Player Box,一旦Player Box进来的接近 AI 控制Box。它将切换回 巡逻状态,如果玩家足够远逃避 未未的定义的愿景。
Step – 1: Set up the scene
以一个平面和两个Box在场景中,如下图所示的设置。
Step – 2: Create and place Game Objects
创建空的游戏对象并将其作为游子点Wanderer Points。放置这些空的游戏对象下,为您选择平面周围。在这里蓝色的Cube是 AI Cube和红一个是玩家控制的Cube。将它们放在距离不够远。
Step – 3: Implement the BoxMovement Script
实现 BoxMovement 脚本来控制玩家的Cube的移动:如下:
public class BoxMovementScript : MonoBehaviour
{
public float speed = 0.1f;
private Vector3 positionVector3;
void Update ()
{
InitializePosition ();
if (Input.GetKey (KeyCode.LeftArrow)) {
GoLeft ();
}
if (Input.GetKey (KeyCode.RightArrow)) {
GoRight ();
}
if (Input.GetKey (KeyCode.UpArrow)) {
GoTop ();
}
if (Input.GetKey (KeyCode.DownArrow)) {
GoDown ();
}
RotateNow ();
}
private void InitializePosition ()
{
positionVector3 = transform.position;
}
private void RotateNow ()
{
Quaternion targetRotation = Quaternion.LookRotation (transform.position - positionVector3);
transform.rotation = targetRotation;
}
private void GoLeft ()
{
transform.position = transform.position + new Vector3 (-speed, 0, 0);
}
private void GoRight ()
{
transform.position = transform.position + new Vector3 (speed, 0, 0);
}
private void GoTop ()
{
transform.position = transform.position + new Vector3 (0, 0, speed);
}
private void GoDown ()
{
transform.position = transform.position + new Vector3 (0, 0, -speed);
}
}
Step – 4: FSM Modelled Script
构建FSM模型脚本:
public class FSM : MonoBehaviour
{
//Player Transform
protected Transform playerTransform;
//Next destination position of the Box
protected Vector3 destPos;
//List of points for patrolling
protected GameObject[] pointList;
protected virtual void Initialize (){
}
protected virtual void FSMUpdate (){
}
protected virtual void FSMFixedUpdate (){
}
void Start ()
{
Initialize ();
}
void Update ()
{
FSMUpdate ();
}
void FixedUpdate ()
{
FSMFixedUpdate ();
}
}
Step – 5: AI Script for the Box
构建 AI 脚本作为Box扩展.
public class BoxFSM : FSM
{
public enum FSMState
{
None,
Patrol,
Chase,
}
//Current state that the Box is in
public FSMState curState;
//Speed of the Box
private float curSpeed;
//Box Rotation Speed
private float curRotSpeed;
//Initialize the Finite state machine for the AI Driven Box
protected override void Initialize ()
{
curState = FSMState.Patrol;
curSpeed = 5.0f;
curRotSpeed = 1.5f;//Get the list of points
pointList = GameObject.FindGameObjectsWithTag ("WandarPoint");
//Set Random destination point for the patrol state first
FindNextPoint ();
//Get the target enemy(Player)
GameObject objPlayer = GameObject.FindGameObjectWithTag ("Player");
playerTransform = objPlayer.transform;
if (!playerTransform)
print ("Player doesn't exist.. Please add one " + "with Tag named 'Player'");
}
//Update each frame
protected override void FSMUpdate ()
{
switch (curState) {
case FSMState.Patrol:
UpdatePatrolState ();
break;
case FSMState.Chase:
UpdateChaseState ();
break;
}
}
protected void UpdatePatrolState ()
{
//Find another random patrol point on reaching the current Point
//point is reached
if (Vector3.Distance (transform.position, destPos) <= 2.5f) {
print ("Reached to the destination point\n" + "calculating the next point");
FindNextPoint ();
}
//Check the distance with player Box
//When the distance is near, transition to chase state
else if (Vector3.Distance (transform.position, playerTransform.position) <= 15.0f) {
print ("Switch to Chase State");
curState = FSMState.Chase;
}//Rotate to the target point
Quaternion targetRotation = Quaternion.LookRotation (destPos - transform.position);
transform.rotation = Quaternion.Slerp (transform.rotation, targetRotation, Time.deltaTime * curRotSpeed);
//Go Forward
transform.Translate (Vector3.forward * Time.deltaTime * curSpeed);
}
protected void FindNextPoint ()
{
print ("Finding next point");
int rndIndex = Random.Range (0, pointList.Length);
float rndRadius = 5.0f;
Vector3 rndPosition = Vector3.zero;
destPos = pointList [rndIndex].transform.position + rndPosition;//Check Range to Move and decide the random point
//as the same as before
if (IsInCurrentRange (destPos)) {
rndPosition = new Vector3 (Random.Range (-rndRadius, rndRadius), 0.0f, Random.Range (-rndRadius, rndRadius));
destPos = pointList [rndIndex].transform.position + rndPosition;
}
}
protected bool IsInCurrentRange (Vector3 pos)
{
float xPos = Mathf.Abs (pos.x - transform.position.x);
float zPos = Mathf.Abs (pos.z - transform.position.z);
if (xPos <= 8 && zPos <= 8)
return true;
return false;
}
protected void UpdateChaseState ()
{
//Set the target position as the player position
destPos = playerTransform.position;
//Check the distance with player Box When
float dist = Vector3.Distance (transform.position, playerTransform.position);//Go back to patrol as player is now too far
if (dist >= 15.0f) {
curState = FSMState.Patrol;
FindNextPoint ();
}
//Rotate to the target point
Quaternion targetRotation = Quaternion.LookRotation (destPos - transform.position);
transform.rotation = Quaternion.Slerp (transform.rotation, targetRotation, Time.deltaTime * curRotSpeed);
//Go Forward
transform.Translate (Vector3.forward * Time.deltaTime * curSpeed);
}
}
此脚本适用于Cube是要跟随玩家,不要忘记把player标记Tag为 Player 和 Tag 为 WandarPoint,现在如 FSMUpdate() 所示的脚本将调用方法,这在子类中重写和它将在每个 update () 上执行。


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
发表于 2017-2-26 17:21 | 显示全部楼层
很不错
发表于 2017-2-26 17:05 | 显示全部楼层
顶顶多好
发表于 2017-2-26 17:20 | 显示全部楼层
真心顶
发表于 2017-2-26 16:46 | 显示全部楼层
说的非常好
发表于 2017-2-26 16:39 | 显示全部楼层
不错不错
发表于 2017-3-7 15:08 | 显示全部楼层
好帖就是要顶
发表于 2017-3-7 14:59 | 显示全部楼层
顶顶多好
发表于 2017-3-7 14:30 | 显示全部楼层
真心顶
发表于 2017-3-7 15:11 | 显示全部楼层
难得一见的好帖
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-4-27 22:10 , Processed in 0.097202 second(s), 27 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表