個別に砲弾を発射できるようにする
個別に砲弾を発射する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ShotShell : MonoBehaviour
{
// ★追加
public int playerNumber;
public GameObject shellPrefab;
public float shotSpeed;
public AudioClip shotSound;
public int shotCount;
public Text shellLabel;
private float timeBetweenShot = 0.35f;
private float timer;
void Start()
{
shellLabel.text = "×" + shotCount;
}
void Update()
{
timer += Time.deltaTime;
// ★改良
// 「GetKeyDown( )メソッド」を「GetButtonDown( )メソッド」に変更する(ポイント)
// その上で個別ID(playerNumber)を活用する
if (Input.GetButtonDown("Shot" + playerNumber) && 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);
}
}
public void AddShell(int amount)
{
shotCount += amount;
if (shotCount > 30)
{
shotCount = 30;
}
shellLabel.text = "×" + shotCount;
}
}
個別に砲弾を発射する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ShotShell : MonoBehaviour
{
// ★追加
public int playerNumber;
public GameObject shellPrefab;
public float shotSpeed;
public AudioClip shotSound;
public int shotCount;
public Text shellLabel;
private float timeBetweenShot = 0.35f;
private float timer;
void Start()
{
shellLabel.text = "×" + shotCount;
}
void Update()
{
timer += Time.deltaTime;
// ★改良
// 「GetKeyDown( )メソッド」を「GetButtonDown( )メソッド」に変更する(ポイント)
// その上で個別ID(playerNumber)を活用する
if (Input.GetButtonDown("Shot" + playerNumber) && 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);
}
}
public void AddShell(int amount)
{
shotCount += amount;
if (shotCount > 30)
{
shotCount = 30;
}
shellLabel.text = "×" + shotCount;
}
}
個別に砲弾を発射できるようにする