//GameObject in attach
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
public GameObject arrowPrefab;
float span = 1.0f;
float delta = 0;
GameObject hpImage;
public Text objFinish;
public Text txtScore;
int iScore = 10;
void Start()
{
this.hpImage = GameObject.Find("hpImage");
txtScore.text = "Count 10";
objFinish.text = "";
}
public void SetScore()
{
iScore -= 2;
txtScore.text = "Count " + iScore.ToString();
}
public void GameOver()
{
objFinish.text = "GameOver";
txtScore.text = "";
Invoke("GameRestart", 3.2f);
}
public void GameRestart()
{
Scene activeScene = SceneManager.GetActiveScene();
SceneManager.LoadScene(activeScene.name);
}
public void DecreaseHp()
{
this.hpImage.GetComponent().fillAmount -= 0.2f;
}
void Update()
{
this.delta += Time.deltaTime;
if (this.delta > this.span)
{
this.delta = 0;
GameObject go = Instantiate(arrowPrefab);
int px = Random.Range(-6, 7);
go.transform.position = new Vector3(px, 7, 0);
}
}
}
///////////////////////////////////
//Player in attach
using UnityEngine;
using UnityEngine.UI;
public class PlayerController : MonoBehaviour
{
public float speed = 10.0f;
public Joystick joystick;
private Rigidbody2D rb;
public float boundX;
GameManager gManager;
public GameObject player;
GameObject hpImage;
public Slider slider;
float maxHp;
float nowHp;
public GameObject[] lifeArray = new GameObject[5];
private int lifePoint = 5;
// Start is called before the first frame update
void Start()
{
rb = GetComponent();
gManager = GameObject.Find("GameObject").GetComponent();
this.hpImage = GameObject.Find("hpImage");
maxHp = 100f;
nowHp = 100f;
//スライダーの最大値の設定
slider.maxValue = maxHp;
//スライダーの現在値の設定
slider.value = nowHp;
}
private void OnTriggerEnter2D(Collider2D coll)
{
if (coll.gameObject.tag == "arrow")
{
gManager.DecreaseHp();
gManager.SetScore();
slider.value -= 20f;
lifeArray[lifePoint - 1].SetActive(false);
lifePoint -= 1;
}
if (hpImage.GetComponent().fillAmount <= 0)
{
player.SetActive(false);
gManager.GameOver();
}
if (slider.value <= 0)
{
player.SetActive(false);
gManager.GameOver();
slider.gameObject.SetActive(false);
}
if (lifePoint <= 0)
{
player.SetActive(false);
gManager.GameOver();
slider.gameObject.SetActive(false);
}
}
void FixedUpdate()
{
var x = transform.position.x + Input.GetAxis("Horizontal") * Time.deltaTime * speed;
if (joystick && Input.GetAxis("Horizontal") == 0)
{
x = transform.position.x + joystick.Horizontal * Time.deltaTime * speed;
}
x = Mathf.Clamp(x, -boundX, boundX);
rb.MovePosition(new Vector2(x, transform.position.y));
}
}
///////////////////////////////////////
//ArrowPrefab in attach
using UnityEngine;
public class Arrow : MonoBehaviour
{
void Update()
{
transform.Translate(0, -0.01f, 0);
// 画面外に出たらオブジェクトを破棄する
if (transform.position.y < -5.0f)
{
Destroy(gameObject);
}
}
}