Unity 2D游戏开发教程之精灵的死亡和重生

Unity 2D游戏开发教程之精灵的死亡和重生

Unity 2D游戏开发快速入门4

Unity 2D游戏开发快速入门4

精灵的死亡和重生

目前为止,游戏项目里的精灵只有Idle和Walking这两种状态。也就是说,无论精灵在游戏里做什么,它都不会进入其它的状态,如死亡。于是我们发现游戏里的精灵,即使是跳入“万丈深渊”,也依然存活,显然这种游戏逻辑无法让人接受。因此,本节就来说明为精灵添加死亡和重生这两种状态的方法,并使用脚本实现这两种状态的逻辑。具体的实现步骤如下:

(1)在Hierarchy视图里,新建一个Empty对象,并命名为Death Trigger,设置其Position属性为(0,0,0)。然后为此对象添加Box Collider 2D组件,并设置此组件的下列属性,如图2-6所示。

 

  • q   选中Is Trigger属性;
  • q   Size:(20,1);
  • q   Center:(0,-2.5);

 

 

 

图2-6  对象上Box Collider 2D组件的属性设置

回看此时的Scene视图,可知此步操作添加了一个绿色线框的矩形,如图2-7所示。我们希望当精灵与此矩形发生接触时,精灵会死亡。

 


图2-7  表示Empty对象范围的矩形框

(2)当精灵死亡以后,要想继续游戏,精灵必须在指定的位置重生才行,而且这个位置在精灵对象重生以后,不会让精灵被动的接触到Death Trigger对象。在Hierarchy视图里,再新建一个Empty对象,并命名为Player Respawn Point,设置其Position属性为(0,1,5,0),也就是说重生的点位于地面正上方的指定位置处,如图2-8所示。

 


图2-8  设置精灵重生点的位置

(3)打开Project视图里的PlayerStateController脚本,将死亡和重生这两种状态加到表示精灵状态的枚举类型中,如下代码中加粗的部分:

 

  • 01     using UnityEngine;
  • 02     using System.Collections;
  • 03
  • 04     public class PlayerStateController : MonoBehaviour
  • 05     {
  • 06              //定义游戏人物的各种状态
  • 07              public enum playerStates
  • 08              {
  • 09                       idle = 0,                                                   //表示空闲
  • 10                       left,                                                           //表示左移
  • 11                       right,                                                                  //表示右移
  • 12                       kill,                                                           //表示死亡
  • 13                       resurrect                                               //表示重生
  • 14              }
  • 15              …                                                                       //省略
  • 16     }

 

(4)在Project视图的Script文件夹里,新建一个C#脚本,命名为DeathTriggerScript,用于实现当精灵与Death Trigger接触时,精灵死亡的逻辑。为此脚本添加下面的代码:

 

  • 01     using UnityEngine;
  • 02     using System.Collections;
  • 03
  • 04     public class DeathTriggerScript : MonoBehaviour
  • 05     {
  • 01              //当精灵进入到Death Trigger的矩形范围内时,调用此函数
  • 02              void OnTriggerEnter2D( Collider2D collidedObject )
  • 03              {
  • 04                       //调用精灵对象上PlayerStateListener脚本组件里的hitDeathTrigger()方法
  • 05                       collidedObject.SendMessage(“hitDeathTrigger”);
  • 06              }
  • 07     }

 

将此脚本赋予Hierarchy视图里的Death Trigger对象。脚本05行,调用的方法hitDeathTrigger()还没有在PlayerStateListener脚本里定义,请将下面的方法定义添加到PlayerStateListener脚本里,定义如下:

 

  • 01     using UnityEngine;
  • 02     using System.Collections;
  • 03
  • 04     [RequireComponent(typeof(Animator))]
  • 05     public class PlayerStateListener : MonoBehaviour
  • 06     {
  • 07              …                                                    //省略
  • 08              public void hitDeathTrigger()
  • 09              {
  • 10                       onStateChange(PlayerStateController.playerStates.kill);
  • 11              }
  • 12     }

 

从方法的定义中可知,它所实现的功能是,修改精灵当前的状态为Kill。

(5)继续为脚本PlayerStateListener添加代码,用于实现当精灵处于死亡和重生状态时,精灵应有的动作,或者说行为,部分脚本PlayerStateListener的代码如下:

 

  • 01     using UnityEngine;
  • 02     using System.Collections;
  • 03
  • 04     [RequireComponent(typeof(Animator))]
  • 05     public class PlayerStateListener : MonoBehaviour
  • 06     {
  • 07              //公有属性
  • 08              public float playerWalkSpeed = 3f;                               //表示精灵移动的速度
  • 09              public GameObject playerRespawnPoint = null;    //表示重生的点
  • 10              //私有属性
  • 11              private Animator playerAnimator = null;                      //表示对象上的Animator组件
  • 12              …                                                                                         //省略
  • 13              //用于检测当前所处的动画状态,在不同的状态下将表现出不同的行为
  • 14              void onStateCycle()
  • 15              {
  • 16                       //表示当前对象的大小
  • 17                       Vector3 localScale = transform.localScale;
  • 18                       //判断当前处于何种状态
  • 19                       switch(currentState)
  • 20                       {
  • 21                       …                                                    //省略
  • 22                       case PlayerStateController.playerStates.kill:
  • 23                                 onStateChange(PlayerStateController.playerStates.resurrect);
  • 24                                 break;
  • 25
  • 26                       case PlayerStateController.playerStates.resurrect:
  • 27                                 onStateChange(PlayerStateController.playerStates.idle);
  • 28                                 break;
  • 29                       }
  • 30              }
  • 31              //当角色的状态发生改变的时候,调用此函数
  • 32              public void onStateChange(PlayerStateController.playerStates newState)
  • 33              {
  • 34                       //如果状态没有发生变化,则无需改变状态
  • 35                       if(newState == currentState)
  • 36                                 return;
  • 37                       //判断精灵能否由当前的动画状态,直接转换为另一个动画状态
  • 38                       if(!checkForValidStatePair(newState))
  • 39                                 return;
  • 40                       //通过修改Parameter中Walking的值,修改精灵当前的状态
  • 41                       switch(newState)
  • 42                       {
  • 43                       …                                                                                //省略
  • 44                       case PlayerStateController.playerStates.kill:
  • 45                                 break;
  • 46                       //让精灵在场景重生对象的位置出现
  • 47                       case PlayerStateController.playerStates.resurrect:
  • 48                                 transform.position = playerRespawnPoint.transform.position;
  • 49                                 transform.rotation = Quaternion.identity;
  • 50
  • 51                                 break;
  • 52                       }
  • 53                       //记录角色当前的状态
  • 54                       currentState = newState;
  • 55              }
  • 56
  • 57              //用于确认当前的动画状态能否直接转换为另一动画状态的函数
  • 58              bool checkForValidStatePair(PlayerStateController.playerStates newState)
  • 59              {
  • 60                       bool returnVal = false;
  • 61
  • 62                       //比较两种动画状态
  • 63                       switch(currentState)
  • 64                       {
  • 65                       …                                                                                                   //省略
  • 66                       //精灵的kill状态只能转换为resurrect状态
  • 67                       case PlayerStateController.playerStates.kill:
  • 68                                 if(newState == PlayerStateController.playerStates.resurrect)
  • 69                                          returnVal = true;
  • 70                                 else
  • 71                                          returnVal = false;
  • 72                                 break;
  • 73                       //精灵的resurrect状态只能转换为idle状态
  • 74                       case PlayerStateController.playerStates. resurrect :
  • 75                                 if(newState == PlayerStateController.playerStates.idle)
  • 76                                          returnVal = true;
  • 77                                 else
  • 78                                          returnVal = false;
  • 79                                 break;
  • 80                       }
  • 81                       return returnVal;
  • 82              }
  • 83              public void hitDeathTrigger()
  • 84              {
  • 85                       onStateChange(PlayerStateController.playerStates.kill);
  • 86              }
  • 87     }

 

对于此脚本,有以下几点需要说明:

 

  • q   脚本09行,添加了一个公有属性,用于表示游戏场景里Player Respawn Point对象的位置。这个属性的值需要在Inspector视图里设置,如图2-9所示。

图2-9  设置Player State Listener脚本组件里的Player Respawn Point属性值
  • q   脚本14行,方法onStateCycle()里添加的代码,说明当精灵进入到kill状态以后,接着会进入resurrect状态;而进入resurrect状态的精灵会接着进入idle状态;
  • q   脚本32行,方法onStateChange()里添加的代码,说明当精灵处于resurrect状态时,精灵将会出现在重生点的位置;
  • q   脚本58行,方法checkForValidStatePair()里添加的代码,说明处于kill状态的精灵只能转换为resurrect状态;而处于resurrect状态的精灵只能转换为idle状态;

 

 

(6)为脚本CameraController添加处理精灵kill和resurrect状态的代码,如下:

 

  • 01     using UnityEngine;
  • 02     using System.Collections;
  • 03
  • 04     public class CameraController : MonoBehaviour
  • 05     {
  • 06              …                                                                       //省略
  • 07              void onStateCycle()
  • 08              {
  • 09                       switch(currentPlayerState)
  • 10                       {
  • 11                                 …                                                    //省略
  • 12                                 case PlayerStateController.playerStates.kill:
  • 13                                          trackPlayer();
  • 14                                 break;
  • 15                                 case PlayerStateController.playerStates.resurrect:
  • 16                                          trackPlayer();
  • 17                                 break;
  • 18                       }
  • 19              }
  • 20     }

 

(7)运行游戏,控制精灵移动至地面外,精灵在下落的过程中与Death Trigger发生接触,精灵死亡;很快的,精灵会在Player Respawn Point对象的位置处重生,如图2-10所示。

 


图2-10  精灵的死亡和重生

本文选自:Unity 2D游戏开发快速入门大学霸内部资料,转载请注明出处,尊重技术尊重IT人!

Comments are closed.