課題4: 新しいアイテムの追加とサウンド設定
目的: ゲームに新しいアイテム(例: ゴールドアップル)を追加し、特別なサウンドと高得点を設定する方法を学ぶ。
目次
背景:
- これまでに リンゴ(Apple) と 爆弾(Bomb) をゲームに実装しました。今回は、さらに特別なアイテム ゴールドアップル(GoldApple) を追加し、取得時に高得点を獲得できるようにします。
 - ゴールドアップルは他のアイテムと異なるサウンドを再生し、GameDirector が適切にスコアを更新できるようにします。
 
ステップ:
1. ゴールドアップルのプレハブを作成
手順:
- モデルの準備:
 
- プレハブの設定:
- Projectウィンドウ内の作成したgoldApplePrefabを選択し、Inspector ウィンドウで以下の設定を行います。
- タグの設定:
- タグを 
"GoldApple"に設定します。まだ存在しない場合は、新しいタグを追加します。 
 - タグを 
 ItemControllerスクリプトの設定:- Item SE にゴールドアップル専用のサウンドファイル(例: 
goldAppleSE)を割り当てます。 
- Item SE にゴールドアップル専用のサウンドファイル(例: 
 
 - タグの設定:
 
 - Projectウィンドウ内の作成したgoldApplePrefabを選択し、Inspector ウィンドウで以下の設定を行います。
 
Unityでは、プレハブはゲームオブジェクトの再利用可能なテンプレートとして機能します。これにより、同じ設定を持つゲームオブジェクトを複数作成する際に時間と労力を節約することができます。プレハブバリアントを使用することで、元のプレハブを変更することなく、異なるシーンやゲーム状況に適したカスタマイズが可能となります。
2. ItemGenerator にゴールドアップルの生成ロジックを追加
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemGenerator : MonoBehaviour
{
    public GameObject applePrefab;
    public GameObject bombPrefab;
    public GameObject goldApplePrefab; // ゴールドアップルのプレハブを追加
    float span = 1.0f;
    float delta = 0;
    int ratio = 2;
    float speed = -0.03f;
    public void SetParameter(float span, float speed, int ratio)
    {
        this.span = span;
        this.speed = speed;
        this.ratio = ratio;
    }
    void Update()
    {
        this.delta += Time.deltaTime;
        if(this.delta > this.span)
        {
            this.delta = 0;
            GameObject item;
            int dice = Random.Range(1, 11);
            if(dice <= this.ratio)
            {
                item = Instantiate(bombPrefab);
            }
            else if(dice == this.ratio + 1) // ゴールドアップルの出現割合を1に設定
            {
                item = Instantiate(goldApplePrefab);
            }
            else
            {
                item = Instantiate(applePrefab);
            }            
            float x = Random.Range(-1, 2);
            float z = Random.Range(-1, 2);
            item.transform.position = new Vector3(x, 4, z);
            item.GetComponent<ItemController>().dropSpeed = this.speed;
        }
    }
}
3. GameDirector にゴールドアップル取得時のポイント追加ロジックを追加
目的: ゴールドアップルを取得した際にポイントが500点増加するように GameDirector を拡張します。
手順:
GameDirector スクリプトの修正:
public class GameDirector : MonoBehaviour
{
    // 既存のコード...
    public void GetGoldApple()
    {
        this.point += 500;
    }
    // 既存のUpdateメソッド内に追加のロジックは不要
}
説明:
GetGoldApple() メソッドの追加:
- ゴールドアップル取得時にポイントを500点増加させるメソッドを追加します。
 
その他のメソッドとの整合性:
- 既存の 
GetApple()とGetBomb()メソッドに加え、GetGoldApple()を追加することで、各アイテムの取得に対応できます。 
4. BasketController でゴールドアップルの取得を処理
- ゴールドアップルにタグ(例: “GoldApple")を設定し、取得時に
GetGoldApple()を呼び出すようにする。 
void OnTriggerEnter(Collider other)
{
    if(other.gameObject.tag == "Apple")
    {
        this.director.GetComponent<GameDirector>().GetApple();
    }
    else if(other.gameObject.tag == "GoldApple")
    {
        this.director.GetComponent<GameDirector>().GetGoldApple();
    }
    else
    {
        this.director.GetComponent<GameDirector>().GetBomb();
    }
    // Destroy(other.gameObject); // ItemController側で破壊するため不要
}
5. テストと確認:
ゴールドアップルが正しく生成され、取得時に特別なサウンドとポイント増加が行われることを確認する。
学習ポイント:
- 新しいアイテムの追加方法。
 - タグを使用したオブジェクトの識別。
 ItemGeneratorでのアイテム生成ロジックの拡張。
訪問数 58 回, 今日の訪問数 1回





ディスカッション
コメント一覧
まだ、コメントがありません