今回やること
- 敵キャラの作成
- 敵の攻撃
- 敵がプレイヤーに向かってくるようにする
- 敵のダメージ処理
敵キャラの作成
![](https://qiita-image-store.s3.amazonaws.com/0/214359/acd57f5a-0e65-8f08-d099-f1790eb14722.png)
![](https://qiita-image-store.s3.amazonaws.com/0/214359/746d4d65-8450-9801-6558-9716f4cb4418.png)
![](https://qiita-image-store.s3.amazonaws.com/0/214359/6492baf9-eb48-3e44-998f-9e8235676e61.png)
![](https://qiita-image-store.s3.amazonaws.com/0/214359/ce173d6a-63a6-f9ef-5c6d-a3046d74cef1.png)
![](https://qiita-image-store.s3.amazonaws.com/0/214359/44c56901-7a5e-4c1a-d536-1f531ff17f40.png)
![](https://qiita-image-store.s3.amazonaws.com/0/214359/025746e9-f8e2-2dcc-e137-df605acbae15.png)
敵の攻撃
Enemyスクリプトには一番上のusing~の部分にも記入します。
using~の記入内容
using UnityEngine.AI;
using~の説明
using UnityEngine.AI;
この後に敵キャラクターにつけるNavMeshAgentを使用する場合につけます。
NavMeshAgentとは敵キャラクターの動きをつけやすくするもので、つけることによって
障害物やステージ内の行ける場所を自動で判別して避けてくれるようになります。
変数を宣言する部分の記入内容
public GameObject target;
NavMeshAgent agent;
Animator animator;
public GameObject Enemyattack;
public Transform enemy;
変数を宣言する部分の説明
public GameObject target;
敵が追従するターゲットを定義します。
今回はプレイヤーに向かってくるようにするのでプレイヤーが該当します。
NavMeshAgent agent;
NavMeshAgentを使うのでagentという名前をつけて定義しています。
もちろん名前は何でも良いです。
Animator animator;
Animatorも使うので定義しています。
Playerスクリプトの時と同じです。
public GameObject Enemyattack;
敵が攻撃する時のエフェクトを定義しています。
public Transform enemy;
敵の座標を定義しています。後々敵の攻撃エフェクトや敵のやられエフェクトを
敵のいる場所に出現させるためです。
void Startの記入内容
agent = GetComponent<NavMeshAgent>();
animator = GetComponent<Animator>();
void Startの説明
agent = GetComponent();
agentにNavMeshAgentの値を代入します。
animator = GetComponent();
同様にanimatorにAnimatorの値を代入します。
void Updateの記入内容
agent.destination = target.transform.position;
void Updateの説明
agent.destination = target.transform.position;
NavMeshAgentの目的地をtargetの座標に決めています。
衝突判定の内容
void OnTriggerEnter(Collider collision)
{
if (collision.gameObject.tag == "Player")
{
StartCoroutine("EnemyAttack");
}
}
衝突判定の説明
void OnTriggerEnter(Collider collision)
衝突判定を定義しています。オブジェクトがオブジェクトに衝突した時の処理は全部ここに書きます。
因みに書き方は
void OnTriggerEnter(Collider 変数の名前)
で始めて{}で閉じます。
if (collision.gameObject.tag == “Player”)
衝突したオブジェクトのタグがPlayerなら…です。
今回はプレイヤーの当たり判定に敵の当たり判定が衝突した場合に敵の攻撃が開始します。
StartCoroutine(“EnemyAttack”);
後に記述するEnemyAttackコルーチンを開始します。
コルーチンの内容
IEnumerator EnemyAttack()
{
animator.SetBool("Attack", true);
yield return new WaitForSeconds(0.05f);
GameObject Enemyattacks = GameObject.Instantiate(Enemyattack) as GameObject;
Enemyattacks.transform.position = enemy.position;
animator.SetBool("Attack", false);
Destroy(Enemyattacks.gameObject, 1);
}
コルーチンの説明
IEnumerator EnemyAttack()
EnemyAttackコルーチンを定義します。
animator.SetBool(“Attack”, true);
Attack変数をtrueにします。
yield return new WaitForSeconds(0.05f);
0.05秒待ちます。
GameObject Enemyattacks = GameObject.Instantiate(Enemyattack) as GameObject;
Enemyattackを複製して複製されたEnemyattackをEnemyattacksと定義しています。
Enemyattacks.transform.position = enemy.position;
Enemyattacksの生成場所をenemyの場所にしています。
animator.SetBool(“Attack”, false);
Attack変数をfalseにします。
Destroy(Enemyattacks.gameObject, 1);
Enemyattacksを1秒後に消します。
完成形のスクリプト
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI;//※4 public class Enemy : MonoBehaviour { public GameObject target;//※4 NavMeshAgent agent;//※4 Animator animator;//※4 public GameObject Enemyattack;//※4 public Transform enemy;//※4 void Start() { agent = GetComponent<NavMeshAgent>();//※4 animator = GetComponent<Animator>();//※4 } void Update() { agent.destination = target.transform.position;//※4 } void OnTriggerEnter(Collider collision)//※4 { if (collision.gameObject.tag == "Player")//※4 { StartCoroutine("EnemyAttack");//※4 } } IEnumerator EnemyAttack()//※4 { animator.SetBool("Attack", true);//※4 yield return new WaitForSeconds(0.05f);//※4 GameObject Enemyattacks = GameObject.Instantiate(Enemyattack) as GameObject;//※4 Enemyattacks.transform.position = enemy.position;//※4 animator.SetBool("Attack", false);//※4 Destroy(Enemyattacks.gameObject, 1);//※4 } }
![](https://qiita-image-store.s3.amazonaws.com/0/214359/d8acf248-9823-c154-e6ad-6ff5fdc026db.png)
![](https://qiita-image-store.s3.amazonaws.com/0/214359/c3fb923d-2614-7680-6332-f0e3d5ff60d3.png)
敵がプレイヤーに向かってくるようにする
![](https://qiita-image-store.s3.amazonaws.com/0/214359/e02520b6-eea0-a8ca-53de-397940ee5818.png)
![](https://qiita-image-store.s3.amazonaws.com/0/214359/5eff5cc3-50a4-455f-114c-e8342d4640e3.png)
![](https://qiita-image-store.s3.amazonaws.com/0/214359/b813369b-513a-0d33-abdd-fa13a67a4373.png)
![](https://qiita-image-store.s3.amazonaws.com/0/214359/56feca35-6500-dafc-cde9-6c96c324c20f.png)
![](https://qiita-image-store.s3.amazonaws.com/0/214359/23b57729-3ac1-f40d-20dc-b6e77b0d4edb.png)
![](https://qiita-image-store.s3.amazonaws.com/0/214359/63bf638b-c2e9-42a9-9822-391c60893251.png)
![](https://qiita-image-store.s3.amazonaws.com/0/214359/e93e7974-8137-f2dd-9ca9-9bf91c73949e.png)
![](https://qiita-image-store.s3.amazonaws.com/0/214359/a11f9866-783a-f2c6-5813-d37807980d12.png)
![](https://qiita-image-store.s3.amazonaws.com/0/214359/b08c638f-d321-eef4-0083-c9c6781840b1.png)
![](https://qiita-image-store.s3.amazonaws.com/0/214359/05d2eb75-754d-eb1e-1ae9-801b7dd576b1.png)
テストプレイをしてみると…
![](https://qiita-image-store.s3.amazonaws.com/0/214359/eb379fb0-d00c-b5ee-9486-d66f7babed7f.gif)
敵が主人公に向かって近づいてきて攻撃してきます。
敵のダメージ処理
更にEnemyスクリプトに追記していきます。
変数を宣言する部分の追記内容
public GameObject Explosion;
変数を宣言する部分の説明
public GameObject Explosion;
敵を倒した時に出現させる爆発を定義しています。
衝突判定の記入内容
void OnTriggerEnter(Collider collision)
{
if (collision.gameObject.tag == "Ball")
{
GameObject Explosions = GameObject.Instantiate(Explosion) as GameObject;
Explosions.transform.position = enemy.position;
Destroy(this.gameObject);
Destroy(Explosions.gameObject, 1);
}
}
衝突判定の説明
void OnTriggerEnter(Collider collision)
衝突判定を定義しています。オブジェクトがオブジェクトに衝突した時の処理は全部ここに書きます。
因みに書き方は
void OnTriggerEnter(Collider 変数の名前)
で始めて{}で閉じます。
if (collision.gameObject.tag == “Ball”)
衝突した際のオブジェクトについているタグがBallだったらという意味です。この場合はプレイヤーが飛ばす弾を指します。
GameObject Explosions = GameObject.Instantiate(Explosion) as GameObject;
Explosionを複製して複製されたExplosionをExplosionsと定義しています。
Explosions.transform.position = enemy.position;
Explosionsの生成場所をenemyの場所にしています。
Destroy(this.gameObject);
このオブジェクトを消しています。
このオブジェクトとはスクリプトを貼り付けているオブジェクトなので敵を指しています。
Destroy(Explosions.gameObject, 1);
Explosionsを1秒後に消します。爆発のエフェクトが出続けると変なので消しています。
完成形のスクリプト
追記内容は※5と表示しています。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;//※4
public class Enemy : MonoBehaviour
{
public GameObject target;//※4
NavMeshAgent agent;//※4
Animator animator;//※4
public GameObject Enemyattack;//※4
public Transform enemy;//※4
public GameObject Explosion;//※5
void Start()
{
agent = GetComponent<NavMeshAgent>();//※4
animator = GetComponent<Animator>();//※4
}
void Update()
{
agent.destination = target.transform.position;//※4
}
void OnTriggerEnter(Collider collision)//※4
{
if (collision.gameObject.tag == "Player")//※4
{
StartCoroutine("EnemyAttack");//※4
}
if (collision.gameObject.tag == "Ball")//※5
{
GameObject Explosions = GameObject.Instantiate(Explosion) as GameObject;//※5
Explosions.transform.position = enemy.position;//※5
Destroy(this.gameObject);//※5
Destroy(Explosions.gameObject, 1);//※5
}
}
IEnumerator EnemyAttack()//※4
{
animator.SetBool("Attack", true);//※4
yield return new WaitForSeconds(0.05f);//※4
GameObject Enemyattacks = GameObject.Instantiate(Enemyattack) as GameObject;//※4
Enemyattacks.transform.position = enemy.position;//※4
animator.SetBool("Attack", false);//※4
Destroy(Enemyattacks.gameObject, 1);//※4
}
}
![](https://qiita-image-store.s3.amazonaws.com/0/214359/13561b1f-88e3-67cb-37d6-c63a7997e166.png)
![](https://i.imgur.com/wJbUV36.png)
![](https://i.imgur.com/yb0aUDH.png)
![](https://i.imgur.com/Bsng7lf.png)
![](https://i.imgur.com/r2uNavC.png)
![](https://i.imgur.com/yqfqCLK.png)
テストプレイをしてみると…
![](EnemyDead.gif](https://qiita-image-store.s3.amazonaws.com/0/214359/06c7a2ee-f6ef-fbe2-1405-e9ff570b673f.gif)
プレイヤーが弾を発射し、それが敵に当たったら倒せました♪
続きはこちら
コメント
コメント一覧 (1件)
[…] 続きはこちら […]