弾切れを発生させる
ライフル銃の弾切れ
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);
}
}
}
弾数を共通化する
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);
}
}
}
ロケットランチャーの弾切れ
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(メモ)
他のコースを見るライフル銃の弾切れ
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);
}
}
}
弾数を共通化する
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);
}
}
}
ロケットランチャーの弾切れ
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);
}
}
}
弾切れを発生させる