砲弾の発射回数に制限を加える(弾切れを発生させる)

弾切れを発生させる
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShotShell : MonoBehaviour
{
public GameObject shellPrefab;
public float shotSpeed;
public AudioClip shotSound;
// ★追加
public int shotCount;
private float timeBetweenShot = 0.35f;
private float timer;
void Update()
{
timer += Time.deltaTime;
if (Input.GetKeyDown(KeyCode.Space) && timer > timeBetweenShot)
{
// ★追加
// (ポイント)returnの働きをおさえる。
if (shotCount < 1)
{
return;
}
// ★追加
shotCount -= 1;
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);
}
}
}



returnの働きをおさえる
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShotShell : MonoBehaviour
{
public GameObject shellPrefab;
public float shotSpeed;
public AudioClip shotSound;
public int shotCount;
private float timeBetweenShot = 0.35f;
private float timer;
void Update()
{
timer += Time.deltaTime;
if (Input.GetKeyDown(KeyCode.Space) && timer > timeBetweenShot)
{
// ★★理解度アップ
// 下記の一行をここに加える。
AudioSource.PlayClipAtPoint(shotSound, transform.position);
if (shotCount < 1)
{
return;
}
shotCount -= 1;
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);
}
}
}

BattleTank(基礎/全31回)
他のコースを見る
弾切れを発生させる
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShotShell : MonoBehaviour
{
public GameObject shellPrefab;
public float shotSpeed;
public AudioClip shotSound;
// ★追加
public int shotCount;
private float timeBetweenShot = 0.35f;
private float timer;
void Update()
{
timer += Time.deltaTime;
if (Input.GetKeyDown(KeyCode.Space) && timer > timeBetweenShot)
{
// ★追加
// (ポイント)returnの働きをおさえる。
if (shotCount < 1)
{
return;
}
// ★追加
shotCount -= 1;
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);
}
}
}



returnの働きをおさえる
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShotShell : MonoBehaviour
{
public GameObject shellPrefab;
public float shotSpeed;
public AudioClip shotSound;
public int shotCount;
private float timeBetweenShot = 0.35f;
private float timer;
void Update()
{
timer += Time.deltaTime;
if (Input.GetKeyDown(KeyCode.Space) && timer > timeBetweenShot)
{
// ★★理解度アップ
// 下記の一行をここに加える。
AudioSource.PlayClipAtPoint(shotSound, transform.position);
if (shotCount < 1)
{
return;
}
shotCount -= 1;
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);
}
}
}

砲弾の発射回数に制限を加える(弾切れを発生させる)