【Unity】自機に消滅のエフェクトをつける。
このブログは、自分が学んだこと、調べたことをアウトプットするために忘備録的に残しているブログとなっております。
間違った情報や私の勘違いなどをお伝えしてしまうこともあるかと思います。
そこのところをご理解いただき、ご覧ください。
また、間違いや勘違いについて、ご指摘ご鞭撻いただければ幸いです。
今回は、自機の消滅・爆発のエフェクトをつけます。
まず、"衝突判定"に必要な設定を自機(Diamond)にを加えます。
1.「ヒエラルキー」の"Diamond"をクリックし、"インスペクター"の"コンポーネントを追加"より、"Rigidbody 2D"を追加します。
物理演算は使わないので、"ボディタイプ"を"キネマティック"に変更します。
2."Circle Collider 2D"を追加します。
"コライダーの編集"をダブルクリックすると、"Collider"の範囲が表示されます。
今回は"半径"を"0.2"、"オフセット"を"Y -0.1"に設定しました。
次に、前回と同じように"爆発のエフェクト"を作成し、自機に設定します。
3.爆発用画像をプロジェクトにドラッグアンドドロップします。
4.アニメーション化したいスプライトをまとめて、「ヒエラルキー」に登録。
この時、元画像の"Daiamond"はアニメーションに含めないほうが良いです。
名称がごちゃごちゃになってしまい、分かりにくくなります。
名称は"DaiamondExplosion.anim"、"DaiamondExplosion.controller"としました。
両方とも「Animations」ホルダーに移動して置きます。
5."DaiamondExplosion.anim"はループさせる必要はないので、「インスペクター」内の"時間をループ"のチェックは外します。
6.「ヒエラルキー」上の名前を"DaiamondExplosion"に変更します。
「PreFabs」フォルダーにドラッグアンドドロップしてPreFab化します。
「ヒエラルキー」上の"DaiamondExplosion"は削除します。
7.「PreFabs」フォルダー内の"DaiamondExplosion.prefab"をクリック、「インスペクター」内の"タグ"に"DaiamondExplosion"を作成して登録します。
必ずしも必要はないようですが、Tagを必ず付けるようにしました。
さらに"コンポーネントを追加"から、"New Script"->"DaiamondExplosionController.cs"を作成します。
※2.参照
8."DaiamondExplosionController.cs"に以下を追加します。
DaiamondExplosionController.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class DiamondExplosion : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{//追加
Destroy(gameObject, 1.0f); //gameObject:自分(DiamondExplosion)自身 1.0secディレイ(待って)で自身を破壊
}// Update is called once per frame
void Update()
{
}
}
9.「Spritsフォルダー」内の"DiamondController.cs"をクリックして編集します。
DiamondController.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class DiamondController : MonoBehaviour
{
//GameObjectの宣言
public GameObject bulletprefab;
public GameObject diamondexplosion; //追加// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
//"Input.GetAxis":Horizontal と Vertical: w、a、s、d と矢印キー
//"Time.deltaTime":現在のフレームと前のフレームの間の時間を提供。
//GameObjectを1秒あたりの単位でその方向に移動します。
//"Mathf.Clamp":与えられた最小 float 値と最大 float 値の範囲に値を制限します。
//"transform.position":3D world space.(ワールド座標:画面内の絶対的位置)
void Update()
{
float dx = Input.GetAxis("Horizontal") * Time.deltaTime * 4f;
float dy = Input.GetAxis("Vertical") * Time.deltaTime * 4f;transform.position = new Vector3(
Mathf.Clamp(transform.position.x + dx, -8.5f, 8.5f),
Mathf.Clamp(transform.position.y + dy, -4.5f, 4.5f),
0f
);//Spaceキーを叩くたびに呼び出す。
if (Input.GetKeyDown(KeyCode.Space))
{
Shoot();
}
}//弾を撃つ関数
private void Shoot()
{
//Diamond(Player機)の中心位置から撃つ
Instantiate(bulletprefab, transform.position, transform.rotation);
}//追記
//自機(プレイヤー機)の爆発
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("Circle")) //Tagが"Circle"だったら
{
Instantiate(diamondexplosion, transform.position, transform.rotation); //自機の爆発エフェクト表示
Destroy(gameObject); //gameObject:自分(Diamond)自身
}
}
}
10.「ヒエラルキー」の"Daiamond"をクリックして"インスペクター"を表示すると、"DiamondController(スクリプト)"に"DaiamondExplosion"の項目が増えています。
そこに「PreFabs」フォルダー内の"DaiamondExplosion.prefab"をドラッグアンドドロップします。
※2.参照
11.さらに、敵機(Circle)もぶつかると同時に爆発させたいので、"CirclePreFabController.cs"にも変更を加えます。
CirclePreFabController.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class CirclePreFabController : MonoBehaviour
{
//GameObjectの宣言
public GameObject circleexplosion;// Start is called before the first frame update
void Start()
{
}// Update is called once per frame
void Update()
{
transform.position += new Vector3(
0f,
-0.5f * Time.deltaTime,
0f
);
}//追記
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("Bullet")) //Tagが"Bullet"だったら
{
Instantiate(circleexplosion, transform.position, transform.rotation); //爆発エフェクト
Destroy(collision.gameObject); //ぶつかったもの(Bullet)
Destroy(gameObject); //gameObject:自分(Circle)自身
}//追加
if (collision.gameObject.CompareTag("Diamond")) //Tagが"Diamond"だったら
{
Instantiate(circleexplosion, transform.position, transform.rotation);
Destroy(gameObject); //gameObject:自分(Circle)自身
}}
}
12.ここまでの作業をセーブして、ゲームを起動して確認します。
・Unityのコンソールにエラーが出てないこと。
・Daiamondに弾が当たると爆発のエフェクトが表示され、消滅すること
自機が爆発したままで、中途半端になりますが、ゲームの終了処理は次回以降で。
今回はここまで。