砲弾の残弾数を画面に表示する
![D89029f1 ba14 44ea b855 f29d7c3ac1d4](https://codegenius.org/uploads/slide/image/2124/d89029f1-ba14-44ea-b855-f29d7c3ac1d4.jpeg)
![729067aa 26df 47a1 ab3f 6f52bee8132f](https://codegenius.org/uploads/slide/image/2125/729067aa-26df-47a1-ab3f-6f52bee8132f.jpeg)
![B7613410 a888 4e66 afb6 3493ea08e6ae](https://codegenius.org/uploads/slide/image/2126/b7613410-a888-4e66-afb6-3493ea08e6ae.jpeg)
![714db737 e320 474f 9420 c9a775f85860](https://codegenius.org/uploads/slide/image/2127/714db737-e320-474f-9420-c9a775f85860.jpeg)
![Aa4a44be 542b 48f4 a5e2 f742eed01853](https://codegenius.org/uploads/slide/image/2128/aa4a44be-542b-48f4-a5e2-f742eed01853.jpeg)
![Fc0b5336 8c05 4183 8b72 c6e8fa779e95](https://codegenius.org/uploads/slide/image/2129/fc0b5336-8c05-4183-8b72-c6e8fa779e95.jpeg)
![C42edf13 a2e9 416c a7c7 2f4604de1d1d](https://codegenius.org/uploads/slide/image/2130/c42edf13-a2e9-416c-a7c7-2f4604de1d1d.jpeg)
![B5be57f1 3696 4f1e b66b 47c5e7f1cf4a](https://codegenius.org/uploads/slide/image/2131/b5be57f1-3696-4f1e-b66b-47c5e7f1cf4a.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;
// ★追加
// Startの「S」は大文字なので注意!
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);
}
}
}
![280a1882 f1e8 4cc8 84be 687f3a32ba80](https://codegenius.org/uploads/slide/image/2132/280a1882-f1e8-4cc8-84be-687f3a32ba80.jpeg)
![D2a9f1b6 42de 407a 8619 24e594526ebe](https://codegenius.org/uploads/slide/image/2133/d2a9f1b6-42de-407a-8619-24e594526ebe.jpeg)
![59a838af 5cc8 4ee4 8795 f4154ceb97c9](https://codegenius.org/uploads/slide/image/2134/59a838af-5cc8-4ee4-8795-f4154ceb97c9.jpeg)
![60daf0b8 43a2 46c5 89be a80bc8b61855](https://codegenius.org/uploads/slide/image/2135/60daf0b8-43a2-46c5-89be-a80bc8b61855.jpeg)
BattleTank(基礎/全31回)
他のコースを見る![D89029f1 ba14 44ea b855 f29d7c3ac1d4](https://codegenius.org/uploads/slide/image/2124/d89029f1-ba14-44ea-b855-f29d7c3ac1d4.jpeg)
![729067aa 26df 47a1 ab3f 6f52bee8132f](https://codegenius.org/uploads/slide/image/2125/729067aa-26df-47a1-ab3f-6f52bee8132f.jpeg)
![B7613410 a888 4e66 afb6 3493ea08e6ae](https://codegenius.org/uploads/slide/image/2126/b7613410-a888-4e66-afb6-3493ea08e6ae.jpeg)
![714db737 e320 474f 9420 c9a775f85860](https://codegenius.org/uploads/slide/image/2127/714db737-e320-474f-9420-c9a775f85860.jpeg)
![Aa4a44be 542b 48f4 a5e2 f742eed01853](https://codegenius.org/uploads/slide/image/2128/aa4a44be-542b-48f4-a5e2-f742eed01853.jpeg)
![Fc0b5336 8c05 4183 8b72 c6e8fa779e95](https://codegenius.org/uploads/slide/image/2129/fc0b5336-8c05-4183-8b72-c6e8fa779e95.jpeg)
![C42edf13 a2e9 416c a7c7 2f4604de1d1d](https://codegenius.org/uploads/slide/image/2130/c42edf13-a2e9-416c-a7c7-2f4604de1d1d.jpeg)
![B5be57f1 3696 4f1e b66b 47c5e7f1cf4a](https://codegenius.org/uploads/slide/image/2131/b5be57f1-3696-4f1e-b66b-47c5e7f1cf4a.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;
// ★追加
// Startの「S」は大文字なので注意!
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);
}
}
}
![280a1882 f1e8 4cc8 84be 687f3a32ba80](https://codegenius.org/uploads/slide/image/2132/280a1882-f1e8-4cc8-84be-687f3a32ba80.jpeg)
![D2a9f1b6 42de 407a 8619 24e594526ebe](https://codegenius.org/uploads/slide/image/2133/d2a9f1b6-42de-407a-8619-24e594526ebe.jpeg)
![59a838af 5cc8 4ee4 8795 f4154ceb97c9](https://codegenius.org/uploads/slide/image/2134/59a838af-5cc8-4ee4-8795-f4154ceb97c9.jpeg)
![60daf0b8 43a2 46c5 89be a80bc8b61855](https://codegenius.org/uploads/slide/image/2135/60daf0b8-43a2-46c5-89be-a80bc8b61855.jpeg)
砲弾の残弾数を画面に表示する