アイテムの作成④(防御シールド)
![543f1344 3c0a 4928 a4db 183e18c1ee06](https://codegenius.org/uploads/slide/image/1199/543f1344-3c0a-4928-a4db-183e18c1ee06.jpeg)
![8141be75 4ee0 43fa 8e87 cbc0c969070b](https://codegenius.org/uploads/slide/image/1200/8141be75-4ee0-43fa-8e87-cbc0c969070b.jpeg)
![34195f3e b268 450b 8fd3 aac56aa21077](https://codegenius.org/uploads/slide/image/1201/34195f3e-b268-450b-8fd3-aac56aa21077.jpeg)
![3a995634 d64a 4129 a2e7 c6e42e56e9d5](https://codegenius.org/uploads/slide/image/1202/3a995634-d64a-4129-a2e7-c6e42e56e9d5.jpeg)
防御シールド
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Shield : MonoBehaviour
{
// ぶつかってくる敵のミサイルを破壊する。
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("EnemyMissile"))
{
Destroy(other.gameObject);
}
}
}
![52eb6171 96bc 4aa3 bcc0 1c9229d07b3d](https://codegenius.org/uploads/slide/image/1203/52eb6171-96bc-4aa3-bcc0-1c9229d07b3d.jpeg)
![B59f1cef d974 4f67 8fe1 32063714e30b](https://codegenius.org/uploads/slide/image/1204/b59f1cef-d974-4f67-8fe1-32063714e30b.jpeg)
![5a26e55c ae6f 4126 bb34 11d3892b00cc](https://codegenius.org/uploads/slide/image/1205/5a26e55c-ae6f-4126-bb34-11d3892b00cc.jpeg)
防御シールドアイテム
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShieldItem : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip getSound;
public GameObject shieldPrefab;
private GameObject player;
private Vector3 pos;
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Missile"))
{
// エフェクトと効果音を発生させる。
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 0.5f);
AudioSource.PlayClipAtPoint(getSound, Camera.main.transform.position);
// プレーヤーの位置情報を取得する。
player = GameObject.FindWithTag("Player");
pos = player.transform.position;
// プレーヤーの側面の位置に2枚の防御シールドを発生させる。
GameObject shieldA = Instantiate(shieldPrefab, new Vector3(pos.x - 0.5f, pos.y, pos.z), Quaternion.identity);
GameObject shieldB = Instantiate(shieldPrefab, new Vector3(pos.x + 0.5f, pos.y, pos.z), Quaternion.identity);
// 発生させた防御シールドをプレーヤーの子供に設定する(親子関係)
// 親子関係にすることで一緒に動くようになる。
shieldA.transform.SetParent(player.transform);
shieldB.transform.SetParent(player.transform);
// 発生させた防御シールドを5秒後に消滅させる。
Destroy(shieldA, 5);
Destroy(shieldB, 5);
// アイテムを破壊する
Destroy(gameObject);
}
}
}
![A1dde7a9 8be2 4b17 b5b9 f162acaf7450](https://codegenius.org/uploads/slide/image/1206/a1dde7a9-8be2-4b17-b5b9-f162acaf7450.jpeg)
![C562a746 7bd6 45d3 b4cc 7c5f706e24d1](https://codegenius.org/uploads/slide/image/1207/c562a746-7bd6-45d3-b4cc-7c5f706e24d1.jpeg)
Danmaku Ⅱ(基礎2/全24回)
他のコースを見る![543f1344 3c0a 4928 a4db 183e18c1ee06](https://codegenius.org/uploads/slide/image/1199/543f1344-3c0a-4928-a4db-183e18c1ee06.jpeg)
![8141be75 4ee0 43fa 8e87 cbc0c969070b](https://codegenius.org/uploads/slide/image/1200/8141be75-4ee0-43fa-8e87-cbc0c969070b.jpeg)
![34195f3e b268 450b 8fd3 aac56aa21077](https://codegenius.org/uploads/slide/image/1201/34195f3e-b268-450b-8fd3-aac56aa21077.jpeg)
![3a995634 d64a 4129 a2e7 c6e42e56e9d5](https://codegenius.org/uploads/slide/image/1202/3a995634-d64a-4129-a2e7-c6e42e56e9d5.jpeg)
防御シールド
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Shield : MonoBehaviour
{
// ぶつかってくる敵のミサイルを破壊する。
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("EnemyMissile"))
{
Destroy(other.gameObject);
}
}
}
![52eb6171 96bc 4aa3 bcc0 1c9229d07b3d](https://codegenius.org/uploads/slide/image/1203/52eb6171-96bc-4aa3-bcc0-1c9229d07b3d.jpeg)
![B59f1cef d974 4f67 8fe1 32063714e30b](https://codegenius.org/uploads/slide/image/1204/b59f1cef-d974-4f67-8fe1-32063714e30b.jpeg)
![5a26e55c ae6f 4126 bb34 11d3892b00cc](https://codegenius.org/uploads/slide/image/1205/5a26e55c-ae6f-4126-bb34-11d3892b00cc.jpeg)
防御シールドアイテム
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShieldItem : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip getSound;
public GameObject shieldPrefab;
private GameObject player;
private Vector3 pos;
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Missile"))
{
// エフェクトと効果音を発生させる。
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 0.5f);
AudioSource.PlayClipAtPoint(getSound, Camera.main.transform.position);
// プレーヤーの位置情報を取得する。
player = GameObject.FindWithTag("Player");
pos = player.transform.position;
// プレーヤーの側面の位置に2枚の防御シールドを発生させる。
GameObject shieldA = Instantiate(shieldPrefab, new Vector3(pos.x - 0.5f, pos.y, pos.z), Quaternion.identity);
GameObject shieldB = Instantiate(shieldPrefab, new Vector3(pos.x + 0.5f, pos.y, pos.z), Quaternion.identity);
// 発生させた防御シールドをプレーヤーの子供に設定する(親子関係)
// 親子関係にすることで一緒に動くようになる。
shieldA.transform.SetParent(player.transform);
shieldB.transform.SetParent(player.transform);
// 発生させた防御シールドを5秒後に消滅させる。
Destroy(shieldA, 5);
Destroy(shieldB, 5);
// アイテムを破壊する
Destroy(gameObject);
}
}
}
![A1dde7a9 8be2 4b17 b5b9 f162acaf7450](https://codegenius.org/uploads/slide/image/1206/a1dde7a9-8be2-4b17-b5b9-f162acaf7450.jpeg)
![C562a746 7bd6 45d3 b4cc 7c5f706e24d1](https://codegenius.org/uploads/slide/image/1207/c562a746-7bd6-45d3-b4cc-7c5f706e24d1.jpeg)
アイテムの作成④(防御シールド)