残弾数を回復させるアイテムの作成
![7d44e7bf 54dd 4911 8d6a d9b6ff7fc16e](https://codegenius.org/uploads/slide/image/2143/7d44e7bf-54dd-4911-8d6a-d9b6ff7fc16e.jpeg)
![94461730 9443 4bdf 9295 2141f381ac74](https://codegenius.org/uploads/slide/image/2144/94461730-9443-4bdf-9295-2141f381ac74.jpeg)
残弾数を増加させるメソッド
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ShotShell : MonoBehaviour
{
public GameObject shellPrefab;
public float shotSpeed;
public AudioClip shotSound;
public int shotCount;
public Text shellLabel;
private float timeBetweenShot = 0.35f;
private float timer;
void Start()
{
shellLabel.text = "×" + shotCount;
}
void Update()
{
timer += Time.deltaTime;
if (Input.GetKeyDown(KeyCode.Space) && timer > timeBetweenShot)
{
if (shotCount < 1)
{
return;
}
shotCount -= 1;
shellLabel.text = "×" + shotCount;
timer = 0.0f;
GameObject shell = Instantiate(shellPrefab, transform.position, Quaternion.identity);
Rigidbody shellRb = shell.GetComponent<Rigidbody>();
shellRb.AddForce(transform.forward * shotSpeed);
Destroy(shell, 3.0f);
AudioSource.PlayClipAtPoint(shotSound, transform.position);
}
}
// ★追加
// 残弾数を増加させるメソッド(関数・命令ブロック)
// 外部からこのメソッドを呼び出せるように「public」をつける(重要ポイント)
// この「AddShellメソッド」を「ShellItem」スクリプトから呼び出す。
public void AddShell(int amount)
{
// shotCountをamount分だけ回復させる
shotCount += amount;
// ただし、残弾数が最大値を超えないようする。(最大値は自由に設定)
if (shotCount > 30)
{
shotCount = 30;
}
// 回復をUIに反映させる。
shellLabel.text = "×" + shotCount;
}
}
![1470908a 6382 4afe 85eb b0a59d1b65fb](https://codegenius.org/uploads/slide/image/2145/1470908a-6382-4afe-85eb-b0a59d1b65fb.jpeg)
残弾数を回復させるアイテム
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShellItem : MonoBehaviour
{
public AudioClip getSound;
public GameObject effectPrefab;
private ShotShell ss;
private int reward = 5; // 弾数をいくつ回復させるかは自由に決定
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
// (重要ポイント)
// Find()メソッドは、「名前」でオブジェクトを探し特定します。
// 「ShotShell」オブジェクトを探し出して、それに付いている「ShotShell」スクリプト(component)のデータを取得。
// 取得したデータを「ss」の箱の中に入れる。
ss = GameObject.Find("ShotShell").GetComponent<ShotShell>();
// ShotShellスクリプトの中に記載されている「AddShellメソッド」を呼び出す。
// rewardで設定した数値分だけ弾数が回復する。
ss.AddShell(reward);
// アイテムを画面から削除する。
Destroy(gameObject);
// アイテムゲット音を出す。
// MainCameraのタグが付いているカメラの側で音を発生させる。
AudioSource.PlayClipAtPoint(getSound, Camera.main.transform.position);
// アイテムゲット時にエフェクトを発生させる。
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
// エフェクトを0.5秒後に消す。
Destroy(effect, 0.5f);
}
}
}
![123eae88 a1b8 4e31 b896 c10265d50f30](https://codegenius.org/uploads/slide/image/2146/123eae88-a1b8-4e31-b896-c10265d50f30.jpeg)
![Ae8a8825 0de9 4dca 8ea4 33dd90da8908](https://codegenius.org/uploads/slide/image/2147/ae8a8825-0de9-4dca-8ea4-33dd90da8908.jpeg)
![76c4e6a6 5b88 4db0 a99a 91bee97339f9](https://codegenius.org/uploads/slide/image/2148/76c4e6a6-5b88-4db0-a99a-91bee97339f9.jpeg)
BattleTank(基礎/全31回)
他のコースを見る![7d44e7bf 54dd 4911 8d6a d9b6ff7fc16e](https://codegenius.org/uploads/slide/image/2143/7d44e7bf-54dd-4911-8d6a-d9b6ff7fc16e.jpeg)
![94461730 9443 4bdf 9295 2141f381ac74](https://codegenius.org/uploads/slide/image/2144/94461730-9443-4bdf-9295-2141f381ac74.jpeg)
残弾数を増加させるメソッド
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ShotShell : MonoBehaviour
{
public GameObject shellPrefab;
public float shotSpeed;
public AudioClip shotSound;
public int shotCount;
public Text shellLabel;
private float timeBetweenShot = 0.35f;
private float timer;
void Start()
{
shellLabel.text = "×" + shotCount;
}
void Update()
{
timer += Time.deltaTime;
if (Input.GetKeyDown(KeyCode.Space) && timer > timeBetweenShot)
{
if (shotCount < 1)
{
return;
}
shotCount -= 1;
shellLabel.text = "×" + shotCount;
timer = 0.0f;
GameObject shell = Instantiate(shellPrefab, transform.position, Quaternion.identity);
Rigidbody shellRb = shell.GetComponent<Rigidbody>();
shellRb.AddForce(transform.forward * shotSpeed);
Destroy(shell, 3.0f);
AudioSource.PlayClipAtPoint(shotSound, transform.position);
}
}
// ★追加
// 残弾数を増加させるメソッド(関数・命令ブロック)
// 外部からこのメソッドを呼び出せるように「public」をつける(重要ポイント)
// この「AddShellメソッド」を「ShellItem」スクリプトから呼び出す。
public void AddShell(int amount)
{
// shotCountをamount分だけ回復させる
shotCount += amount;
// ただし、残弾数が最大値を超えないようする。(最大値は自由に設定)
if (shotCount > 30)
{
shotCount = 30;
}
// 回復をUIに反映させる。
shellLabel.text = "×" + shotCount;
}
}
![1470908a 6382 4afe 85eb b0a59d1b65fb](https://codegenius.org/uploads/slide/image/2145/1470908a-6382-4afe-85eb-b0a59d1b65fb.jpeg)
残弾数を回復させるアイテム
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShellItem : MonoBehaviour
{
public AudioClip getSound;
public GameObject effectPrefab;
private ShotShell ss;
private int reward = 5; // 弾数をいくつ回復させるかは自由に決定
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
// (重要ポイント)
// Find()メソッドは、「名前」でオブジェクトを探し特定します。
// 「ShotShell」オブジェクトを探し出して、それに付いている「ShotShell」スクリプト(component)のデータを取得。
// 取得したデータを「ss」の箱の中に入れる。
ss = GameObject.Find("ShotShell").GetComponent<ShotShell>();
// ShotShellスクリプトの中に記載されている「AddShellメソッド」を呼び出す。
// rewardで設定した数値分だけ弾数が回復する。
ss.AddShell(reward);
// アイテムを画面から削除する。
Destroy(gameObject);
// アイテムゲット音を出す。
// MainCameraのタグが付いているカメラの側で音を発生させる。
AudioSource.PlayClipAtPoint(getSound, Camera.main.transform.position);
// アイテムゲット時にエフェクトを発生させる。
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
// エフェクトを0.5秒後に消す。
Destroy(effect, 0.5f);
}
}
}
![123eae88 a1b8 4e31 b896 c10265d50f30](https://codegenius.org/uploads/slide/image/2146/123eae88-a1b8-4e31-b896-c10265d50f30.jpeg)
![Ae8a8825 0de9 4dca 8ea4 33dd90da8908](https://codegenius.org/uploads/slide/image/2147/ae8a8825-0de9-4dca-8ea4-33dd90da8908.jpeg)
![76c4e6a6 5b88 4db0 a99a 91bee97339f9](https://codegenius.org/uploads/slide/image/2148/76c4e6a6-5b88-4db0-a99a-91bee97339f9.jpeg)
残弾数を回復させるアイテムの作成