【バイオハザード風のゲームを作ってみよう。その8】
今回も下記動画を参考にやっていきます。
youtu.be
今回は、ゾンビをAI動作させる準備をしていきます。
・キャラクターとアニメーションはいつも通りmixamoからダウンロードしました。
・各スクリプトは下記。
using System.Collections; using System.Collections.Generic; using UnityEngine; public class ZombieManager : MonoBehaviour //EP8追加 ゾンビオブジェクトにアタッチ { //The state this character begins on public IdleState startingState; //The state this character is currently on [SerializeField] private State currentState; private void Awake() { currentState = startingState; } private void FixedUpdate() { HandleStateMachine(); } private void HandleStateMachine() { State nextState; if (currentState != null) { nextState = currentState.Tick(); if (nextState != null) { currentState = nextState; } } } }
using System.Collections; using System.Collections.Generic; using UnityEngine; public class State : MonoBehaviour //EP8追加 { //this is the base class for all future state //抽象メソッド。なお、virtualの場合、オーバーライドしなくてもエラーにならなく、実際の処理を記述することもできる。 public virtual State Tick() { Debug.Log("Running State"); return this; } }
using System.Collections; using System.Collections.Generic; using UnityEngine; public class IdleState : State //EP8追加 ゾンビ直下のstatesオブジェクトにアタッチ { PurseTargetState purseTargetState; //we make our character idle until they find a potential target //if a target is found we proceed to the "PursueTarget" state //if no target is found we remain in the idle position [SerializeField] bool hasTarget; private void Awake() { purseTargetState = GetComponent<PurseTargetState>(); } public override State Tick() { //LOGIC TO FIND A TARGET GOES HERE. if (hasTarget) { Debug.Log("we have found a target!"); return purseTargetState; } else { Debug.Log("We have no target yet."); return this; } } }
using System.Collections; using System.Collections.Generic; using UnityEngine; public class PurseTargetState : State //EP8追加 ゾンビ直下のstatesオブジェクトにアタッチ { public override State Tick() { //logic to follow current target goes here Debug.Log("Running pursue target state"); return this; } }
今回は以上です。