砲弾の発射回数に制限を加える(弾切れを発生させる)
![3e396763 46ee 4051 a80c 48a87e7c2f55](https://codegenius.org/uploads/slide/image/2109/3e396763-46ee-4051-a80c-48a87e7c2f55.jpeg)
弾切れを発生させる
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);
}
}
}
![Cb668cd1 89c7 4bea 890e 1ac59fbab392](https://codegenius.org/uploads/slide/image/2110/cb668cd1-89c7-4bea-890e-1ac59fbab392.jpeg)
![C29286bd b6b7 4cef ab13 e8191e1f995c](https://codegenius.org/uploads/slide/image/2111/c29286bd-b6b7-4cef-ab13-e8191e1f995c.jpeg)
![7c6c86a8 38f1 4471 b97c e1c86095dbe2](https://codegenius.org/uploads/slide/image/2112/7c6c86a8-38f1-4471-b97c-e1c86095dbe2.jpeg)
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);
}
}
}
![Df08e163 2611 41e9 9028 3bd0d42e5a11](https://codegenius.org/uploads/slide/image/2113/df08e163-2611-41e9-9028-3bd0d42e5a11.jpeg)
BattleTank(基礎/全31回)
他のコースを見る![3e396763 46ee 4051 a80c 48a87e7c2f55](https://codegenius.org/uploads/slide/image/2109/3e396763-46ee-4051-a80c-48a87e7c2f55.jpeg)
弾切れを発生させる
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);
}
}
}
![Cb668cd1 89c7 4bea 890e 1ac59fbab392](https://codegenius.org/uploads/slide/image/2110/cb668cd1-89c7-4bea-890e-1ac59fbab392.jpeg)
![C29286bd b6b7 4cef ab13 e8191e1f995c](https://codegenius.org/uploads/slide/image/2111/c29286bd-b6b7-4cef-ab13-e8191e1f995c.jpeg)
![7c6c86a8 38f1 4471 b97c e1c86095dbe2](https://codegenius.org/uploads/slide/image/2112/7c6c86a8-38f1-4471-b97c-e1c86095dbe2.jpeg)
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);
}
}
}
![Df08e163 2611 41e9 9028 3bd0d42e5a11](https://codegenius.org/uploads/slide/image/2113/df08e163-2611-41e9-9028-3bd0d42e5a11.jpeg)
砲弾の発射回数に制限を加える(弾切れを発生させる)