★Munechika Yonemoto

(2025年12月8日)

<BOSSのHPによって、表示、非表示が切り替わる>

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Boss : MonoBehaviour
{
    //public GameObject body;
    public GameObject effectPrefab;
    public GameObject effectPrefab2;
    public int objectHP;

    public float shotSpeed;
    public GameObject[] enemyShellPrefabs;
    public AudioClip shotSound;
    private int count;

    public GameObject target;
    private float dis;
    private int num = 0;
    public MeshRenderer mesh;


    private void Update()
    {
        if (target)
        {
            dis = Vector3.Distance(transform.position, target.transform.position);
            if (dis < 50)
            {
                transform.LookAt(target.transform);
            }
        }
        count += 1;
        if (count % 100 == 0)
        {
            num = (num + 1) % 3;
            if (num == 0 || num == 1)
            {
                GameObject enemyShell = Instantiate(enemyShellPrefabs[0], transform.position, Quaternion.identity);
                Rigidbody enemyShellRb = enemyShell.GetComponent();
                enemyShellRb.AddForce(transform.forward * shotSpeed);
                AudioSource.PlayClipAtPoint(shotSound, transform.position);
                Destroy(enemyShell, 3.0f);
            }
            else
            {
                GameObject enemyShell = Instantiate(enemyShellPrefabs[1], transform.position, Quaternion.Euler(90, 0, 0));
                //Rigidbody enemyShellRb = enemyShell.GetComponent();
                //enemyShellRb.AddForce(transform.forward * shotSpeed);
                AudioSource.PlayClipAtPoint(shotSound, transform.position);
                Destroy(enemyShell, 3.0f);
            }
        }

        // ★12月8日追加
        if(objectHP >= 6)
        {
            mesh.enabled = true;
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        // (ポイントメモ)ミサイルがあった時にしか実行されない。
        if (other.CompareTag("Missile"))
        {
            objectHP -= 1;

            // ★12月8日改良
            if (objectHP <= 2)
            {
                mesh.enabled = false;
            }

            // ★12月8日削除
            /*
            if (objectHP == 6)
            {
                print("ok");
                mesh.enabled = true;
                print("ok12");
            }
            */

            if (objectHP > 0)
            {
                Destroy(other.gameObject);
                GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
                Destroy(effect, 2.0f);
            }
            else
            {
                Destroy(other.gameObject);
                GameObject effect2 = Instantiate(effectPrefab2, transform.position, Quaternion.identity);
                Destroy(effect2, 2.0f);
                Destroy(this.gameObject);
            }
        }
    }
    public void AddHP()
    {
        objectHP += 1;
    }
}

(2025年11月3日)

<敵を破壊したら一時停止になるエラーの修正>

MoveEnemyAから下記スクリプトを削除すればOK

<ShotSpeedUpアイテムがプレハブフォルダに存在しない>

 

 


(2025年7月28日)

<4個のバリアを破壊したらボスにダメージが入るようになる>

・「DestroyBoss」スクリプトの改良

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DestroyBoss : MonoBehaviour
{
    public GameObject effectPrefab;
    public GameObject effectPrefab2;
    public int objectHP;
    public GameObject itemPrefab;
    public int scoreValue;
    public int damage = 1;
    private ScoreManager sm;

    // ★変更はここだけ!(publicに変更)
    public bool barrier = true;

    private void Start()
    {
        sm = GameObject.Find("ScoreLabel").GetComponent<ScoreManager>();
    }

    private void OnTriggerEnter(Collider other)
    {
        if (barrier == true)
        {
            return;
        }

        if (other.CompareTag("Shell"))
        {
            objectHP -= damage;

            if (objectHP > 0)
            {
                Destroy(other.gameObject);
                GameObject effect = Instantiate(effectPrefab, other.transform.position, Quaternion.identity);
                Destroy(effect, 2.0f);
            }
            else
            {
                sm.AddScore(scoreValue);
                Destroy(other.gameObject);
                GameObject effect2 = Instantiate(effectPrefab2, other.transform.position, Quaternion.identity);
                Destroy(this.gameObject);

                if (itemPrefab)
                {
                    Vector3 pos = transform.position;
                    Instantiate(itemPrefab, new Vector3(pos.x, pos.y + 0.6f, pos.z), Quaternion.identity);
                }
            }
        }
    }
}

・新規にC#スクリプトの作成

・名前を「GameController」に変更

・下記のコードを書いてチェック

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameController : MonoBehaviour
{
    private GameObject[] barriers;
    public GameObject bossBariier;
    public DestroyBoss destroyBoss;
    public AudioClip sound;
    private bool isOk = false;

    void Update()
    {
        barriers = GameObject.FindGameObjectsWithTag("Barrier.");

        if (barriers.Length == 0)
        {
            if(isOk)
            {
                return;
            }

            // ボスのバリアを破壊する
            Destroy(bossBariier.gameObject);

            // ボスにダメージが入るようにする
            destroyBoss.barrier = false;

            // バリアが壊れる音を出す
            AudioSource.PlayClipAtPoint(sound, Camera.main.transform.position);

            isOk = true;
        }
    }
}

・新規にCreate Emptyオブジェクトを作成

・名前を「GameController」に変更

・これにスクリプトを追加

・下記3つの設定を行う

・Boss Barrierの空欄にはBoss2の中にある「Barrier.」オブジェクトを追加

・Destroy Bossの空欄には「Boss2」オブジェクトを追加

・Soundの空欄には「バリアが壊れる音」を追加

・タグの設定

 

 


(2025年3月3日)

<Keyオブジェクトに触れると、5秒間だけ無限ジャンプが可能になる>

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Boll : MonoBehaviour
{
    public float moveSpeed;
    private Rigidbody rb;
    public AudioClip coinGet;
    public float jumpSpeed;
    private bool isjumping = false;
    private int coinCount = 0;
    private Vector3 pos;
    public GameObject[] coinIcons;
    public GameObject key;
    public AudioClip keysound;

    // ★追加(無限ジャンプ)
    private bool isMugen = false;

    void Start()
    {
        rb = GetComponent();
    }

    void Update()
    {
        pos = transform.position;
        if (pos.y < -10)
        {
            SceneManager.LoadScene("GameOver");
        }
        float moveH = Input.GetAxis("Horizontal");
        float moveV = Input.GetAxis("Vertical");
        Vector3 movement = new Vector3(moveH, 0, moveV);
        rb.AddForce(movement * moveSpeed);

        if (Input.GetButtonDown("Jump") && isjumping == false)
        {
            rb.velocity = Vector3.up * jumpSpeed;

            // ★追加(無限ジャンプ)
            if (isMugen == false)
            {
                isjumping = true;
            }
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Coin"))
        {
            Destroy(other.gameObject);
            AudioSource.PlayClipAtPoint(coinGet, Camera.main.transform.position);
            coinCount += 1;
            coinIcons[coinCount - 1].SetActive(false);
            if (coinCount == 1)
            {
                key.SetActive(true);
                AudioSource.PlayClipAtPoint(keysound, Camera.main.transform.position);
            }

            if (coinCount == 3)
            {
                SceneManager.LoadScene("GameClear");
            }
        }
        //キーというタグに触れたら無限ジャンプできる
        if (other.CompareTag("Key"))
        {
            // ★変更(無限ジャンプ)
            Destroy(other.gameObject);

            isMugen = true;

            // ボールの色を黒に変える
            this.gameObject.GetComponent<MeshRenderer>().material.color = Color.black;

            Invoke("ResetMugen", 5f); // 5秒間だけ無限ジャンプできる
        }
    }

    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.CompareTag("Floor"))
        {
            isjumping = false;
        }
    }

    // ★追加(無限ジャンプ)
    void ResetMugen()
    {
        isMugen = false;

        // ボールの色を緑に戻す
        this.gameObject.GetComponent<MeshRenderer>().material.color = Color.green;
    }
}