残弾数を回復させるアイテムの作成


残弾数を回復させる
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class ShotShell : MonoBehaviour
{
public float shotSpeed;
public GameObject shellPrefab;
public AudioClip shotSound;
private float interval = 0.75f;
private float timer = 0;
// ★変更(privateに変更する)
private int shotCount;
public TextMeshProUGUI shellLabel;
// ★追加
// 弾数の最大値の設定(最大値は自由)
private int shotMaxCount = 20;
private void Start()
{
// ★追加
shotCount = shotMaxCount;
shellLabel.text = "" + shotCount;
}
void Update()
{
timer += Time.deltaTime;
if (Input.GetKeyDown(KeyCode.Space) && timer > interval && shotCount > 0)
{
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」をつける(重要)
// このメソッドをShellItemスクリプトから呼び出す。
public void AddShell(int amount)
{
// shotCountをamount分だけ回復させる。
shotCount += amount;
// ただし、最大値を超えないようにする。
if(shotCount > shotMaxCount)
{
shotCount = shotMaxCount;
}
// 回復をUIに反映させる。
shellLabel.text = "" + shotCount;
}
}

残弾数を回復させる
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.CompareTag("Player"))
{
// Find()の復習!
ss = GameObject.Find("ShotShell").GetComponent<ShotShell>();
// ★ここの意味をおさえよう!(重要ポイント)
// メソッド経由で変数の値を変更する。
// ShotShellスクリプトの中に記載されている「AddShellメソッド」を呼び出す。
// rewardで設定した数値分だけ弾数が回復する。
ss.AddShell(reward);
Destroy(gameObject);
AudioSource.PlayClipAtPoint(getSound, transform.position);
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 0.5f);
}
}
}



【2021版】BattleTank(基礎/全33回)
他のコースを見る

残弾数を回復させる
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class ShotShell : MonoBehaviour
{
public float shotSpeed;
public GameObject shellPrefab;
public AudioClip shotSound;
private float interval = 0.75f;
private float timer = 0;
// ★変更(privateに変更する)
private int shotCount;
public TextMeshProUGUI shellLabel;
// ★追加
// 弾数の最大値の設定(最大値は自由)
private int shotMaxCount = 20;
private void Start()
{
// ★追加
shotCount = shotMaxCount;
shellLabel.text = "" + shotCount;
}
void Update()
{
timer += Time.deltaTime;
if (Input.GetKeyDown(KeyCode.Space) && timer > interval && shotCount > 0)
{
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」をつける(重要)
// このメソッドをShellItemスクリプトから呼び出す。
public void AddShell(int amount)
{
// shotCountをamount分だけ回復させる。
shotCount += amount;
// ただし、最大値を超えないようにする。
if(shotCount > shotMaxCount)
{
shotCount = shotMaxCount;
}
// 回復をUIに反映させる。
shellLabel.text = "" + shotCount;
}
}

残弾数を回復させる
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.CompareTag("Player"))
{
// Find()の復習!
ss = GameObject.Find("ShotShell").GetComponent<ShotShell>();
// ★ここの意味をおさえよう!(重要ポイント)
// メソッド経由で変数の値を変更する。
// ShotShellスクリプトの中に記載されている「AddShellメソッド」を呼び出す。
// rewardで設定した数値分だけ弾数が回復する。
ss.AddShell(reward);
Destroy(gameObject);
AudioSource.PlayClipAtPoint(getSound, transform.position);
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 0.5f);
}
}
}



残弾数を回復させるアイテムの作成