弾切れを発生させる
![8faf0724 47ed 46e1 ad6c 998b4311b0a5](https://codegenius.org/uploads/slide/image/2648/8faf0724-47ed-46e1-ad6c-998b4311b0a5.jpeg)
![4e4c9ede 36db 48fb 83e5 6eeb5b717e9e](https://codegenius.org/uploads/slide/image/2649/4e4c9ede-36db-48fb-83e5-6eeb5b717e9e.jpeg)
ライフル銃の弾切れ
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShotBullet : MonoBehaviour {
public GameObject bulletPrefab;
public AudioClip shotSound;
public float shotSpeed;
// ★追加
public int shotBulletCount;
void Update () {
if (Input.GetKeyDown (KeyCode.Space)) {
// ★追加
// 「return」の意味を復習しましょう!
if (shotBulletCount < 1) {
return;
}
// ★追加
shotBulletCount -= 1;
GameObject bullet = (GameObject)Instantiate (bulletPrefab, transform.position, Quaternion.Euler (transform.parent.eulerAngles.x, transform.parent.eulerAngles.y, 0));
Rigidbody bulletRb = bullet.GetComponent<Rigidbody> ();
bulletRb.AddForce (transform.forward * shotSpeed);
AudioSource.PlayClipAtPoint (shotSound, Camera.main.transform.position);
Destroy (bullet, 2.0f);
}
}
}
![B5685a25 550e 49ad bf19 8f9b98aa20f9](https://codegenius.org/uploads/slide/image/2650/b5685a25-550e-49ad-bf19-8f9b98aa20f9.jpeg)
![31579b9f 8201 4eee 8e58 c8068c0e53ba](https://codegenius.org/uploads/slide/image/2651/31579b9f-8201-4eee-8e58-c8068c0e53ba.jpeg)
弾数を共通化する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShotBullet : MonoBehaviour {
public GameObject bulletPrefab;
public AudioClip shotSound;
public float shotSpeed;
// ★追加
// 「static」の意味をグーグルで調べてみましょう!
public static int shotBulletCount = 10;
void Update () {
if (Input.GetKeyDown (KeyCode.Space)) {
if (shotBulletCount < 1) {
return;
}
shotBulletCount -= 1;
GameObject bullet = (GameObject)Instantiate (bulletPrefab, transform.position, Quaternion.Euler (transform.parent.eulerAngles.x, transform.parent.eulerAngles.y, 0));
Rigidbody bulletRb = bullet.GetComponent<Rigidbody> ();
bulletRb.AddForce (transform.forward * shotSpeed);
AudioSource.PlayClipAtPoint (shotSound, Camera.main.transform.position);
Destroy (bullet, 2.0f);
}
}
}
![C67808cf b173 49f3 b0f5 69e84b811446](https://codegenius.org/uploads/slide/image/2652/c67808cf-b173-49f3-b0f5-69e84b811446.jpeg)
![Cf2382c2 8f7c 45fa ba5a d9c89173868b](https://codegenius.org/uploads/slide/image/2653/cf2382c2-8f7c-45fa-ba5a-d9c89173868b.jpeg)
![16283c4a 8487 4d67 86ac fd643e05ce37](https://codegenius.org/uploads/slide/image/2654/16283c4a-8487-4d67-86ac-fd643e05ce37.jpeg)
ロケットランチャーの弾切れ
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShotRocket : MonoBehaviour {
public GameObject rocketPrefab;
public AudioClip shotSound;
public float rocketSpeed;
private float timeBetweenShot = 1.2f;
private float timer;
// ★追加
public static int shotRocketCount = 10;
void Update () {
timer += Time.deltaTime;
if (Input.GetKeyDown (KeyCode.Space) && timer > timeBetweenShot) {
// ★追加
if (shotRocketCount < 1) {
return;
}
// ★追加
shotRocketCount -= 1;
timer = 0.0f;
GameObject rocket = (GameObject)Instantiate (rocketPrefab, transform.position, Quaternion.Euler (transform.parent.eulerAngles.x, transform.parent.eulerAngles.y, 0));
Rigidbody rocketRb = rocket.GetComponent<Rigidbody> ();
rocketRb.AddForce (transform.forward * rocketSpeed);
AudioSource.PlayClipAtPoint (shotSound, Camera.main.transform.position);
Destroy (rocket, 3.0f);
}
}
}
EscapeCombat(メモ)
他のコースを見る![8faf0724 47ed 46e1 ad6c 998b4311b0a5](https://codegenius.org/uploads/slide/image/2648/8faf0724-47ed-46e1-ad6c-998b4311b0a5.jpeg)
![4e4c9ede 36db 48fb 83e5 6eeb5b717e9e](https://codegenius.org/uploads/slide/image/2649/4e4c9ede-36db-48fb-83e5-6eeb5b717e9e.jpeg)
ライフル銃の弾切れ
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShotBullet : MonoBehaviour {
public GameObject bulletPrefab;
public AudioClip shotSound;
public float shotSpeed;
// ★追加
public int shotBulletCount;
void Update () {
if (Input.GetKeyDown (KeyCode.Space)) {
// ★追加
// 「return」の意味を復習しましょう!
if (shotBulletCount < 1) {
return;
}
// ★追加
shotBulletCount -= 1;
GameObject bullet = (GameObject)Instantiate (bulletPrefab, transform.position, Quaternion.Euler (transform.parent.eulerAngles.x, transform.parent.eulerAngles.y, 0));
Rigidbody bulletRb = bullet.GetComponent<Rigidbody> ();
bulletRb.AddForce (transform.forward * shotSpeed);
AudioSource.PlayClipAtPoint (shotSound, Camera.main.transform.position);
Destroy (bullet, 2.0f);
}
}
}
![B5685a25 550e 49ad bf19 8f9b98aa20f9](https://codegenius.org/uploads/slide/image/2650/b5685a25-550e-49ad-bf19-8f9b98aa20f9.jpeg)
![31579b9f 8201 4eee 8e58 c8068c0e53ba](https://codegenius.org/uploads/slide/image/2651/31579b9f-8201-4eee-8e58-c8068c0e53ba.jpeg)
弾数を共通化する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShotBullet : MonoBehaviour {
public GameObject bulletPrefab;
public AudioClip shotSound;
public float shotSpeed;
// ★追加
// 「static」の意味をグーグルで調べてみましょう!
public static int shotBulletCount = 10;
void Update () {
if (Input.GetKeyDown (KeyCode.Space)) {
if (shotBulletCount < 1) {
return;
}
shotBulletCount -= 1;
GameObject bullet = (GameObject)Instantiate (bulletPrefab, transform.position, Quaternion.Euler (transform.parent.eulerAngles.x, transform.parent.eulerAngles.y, 0));
Rigidbody bulletRb = bullet.GetComponent<Rigidbody> ();
bulletRb.AddForce (transform.forward * shotSpeed);
AudioSource.PlayClipAtPoint (shotSound, Camera.main.transform.position);
Destroy (bullet, 2.0f);
}
}
}
![C67808cf b173 49f3 b0f5 69e84b811446](https://codegenius.org/uploads/slide/image/2652/c67808cf-b173-49f3-b0f5-69e84b811446.jpeg)
![Cf2382c2 8f7c 45fa ba5a d9c89173868b](https://codegenius.org/uploads/slide/image/2653/cf2382c2-8f7c-45fa-ba5a-d9c89173868b.jpeg)
![16283c4a 8487 4d67 86ac fd643e05ce37](https://codegenius.org/uploads/slide/image/2654/16283c4a-8487-4d67-86ac-fd643e05ce37.jpeg)
ロケットランチャーの弾切れ
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShotRocket : MonoBehaviour {
public GameObject rocketPrefab;
public AudioClip shotSound;
public float rocketSpeed;
private float timeBetweenShot = 1.2f;
private float timer;
// ★追加
public static int shotRocketCount = 10;
void Update () {
timer += Time.deltaTime;
if (Input.GetKeyDown (KeyCode.Space) && timer > timeBetweenShot) {
// ★追加
if (shotRocketCount < 1) {
return;
}
// ★追加
shotRocketCount -= 1;
timer = 0.0f;
GameObject rocket = (GameObject)Instantiate (rocketPrefab, transform.position, Quaternion.Euler (transform.parent.eulerAngles.x, transform.parent.eulerAngles.y, 0));
Rigidbody rocketRb = rocket.GetComponent<Rigidbody> ();
rocketRb.AddForce (transform.forward * rocketSpeed);
AudioSource.PlayClipAtPoint (shotSound, Camera.main.transform.position);
Destroy (rocket, 3.0f);
}
}
}
弾切れを発生させる