コインの残り枚数を表示する
![9c0e0179 81d0 47b7 9421 6946693413fe](https://codegenius.org/uploads/slide/image/5786/9c0e0179-81d0-47b7-9421-6946693413fe.jpeg)
![7b27817d 49a1 41e6 9ece ec300efb2d5f](https://codegenius.org/uploads/slide/image/5787/7b27817d-49a1-41e6-9ece-ec300efb2d5f.jpeg)
![9997f76f b0c3 4f68 8414 ed325076b110](https://codegenius.org/uploads/slide/image/5788/9997f76f-b0c3-4f68-8414-ed325076b110.jpeg)
![0d0d7024 2654 490a ba8e e59bc72cb4b4](https://codegenius.org/uploads/slide/image/5789/0d0d7024-2654-490a-ba8e-e59bc72cb4b4.jpeg)
![69a50f48 36b2 45ea 9a83 b9133131c0e4](https://codegenius.org/uploads/slide/image/5790/69a50f48-36b2-45ea-9a83-b9133131c0e4.jpeg)
![Af5a4829 6d34 4201 abe0 30606d9e3ab5](https://codegenius.org/uploads/slide/image/5791/af5a4829-6d34-4201-abe0-30606d9e3ab5.jpeg)
![B3575b8f 89cb 4618 b28f e896c54b334e](https://codegenius.org/uploads/slide/image/5792/b3575b8f-89cb-4618-b28f-e896c54b334e.jpeg)
![Cb853347 6ff4 4b9b b109 f93df6580aae](https://codegenius.org/uploads/slide/image/5793/cb853347-6ff4-4b9b-b109-f93df6580aae.jpeg)
![68345ab0 a029 47b4 b540 3b252ca24790](https://codegenius.org/uploads/slide/image/5794/68345ab0-a029-47b4-b540-3b252ca24790.jpeg)
![1e203cdf a601 48c5 af3a 31c351be31d3](https://codegenius.org/uploads/slide/image/5795/1e203cdf-a601-48c5-af3a-31c351be31d3.jpeg)
![1e870128 9df0 4302 bfd0 73f401642276](https://codegenius.org/uploads/slide/image/5796/1e870128-9df0-4302-bfd0-73f401642276.jpeg)
![38eabed8 e351 449d b3ae 60140d7f0706](https://codegenius.org/uploads/slide/image/5797/38eabed8-e351-449d-b3ae-60140d7f0706.jpeg)
![C44a074d d8bb 41b6 962b f2da7f5a9fd9](https://codegenius.org/uploads/slide/image/5798/c44a074d-d8bb-41b6-962b-f2da7f5a9fd9.jpeg)
コインアイコンを表示する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Ball : MonoBehaviour
{
public float moveSpeed;
private Rigidbody rb;
public AudioClip coinGet;
public float jumpSpeed;
private bool isJumping = false;
// ★修正(privateをpublicに変更する)
public int coinCount = 0;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
float moveH = Input.GetAxis("Horizontal");
float moveV = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveH, 0, moveV);
rb.AddForce(movement * moveSpeed);
if (Input.GetButtonDown("Jump") && isJumping == false)
{
rb.velocity = Vector3.up * jumpSpeed;
isJumping = true;
}
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Coin"))
{
Destroy(other.gameObject);
AudioSource.PlayClipAtPoint(coinGet, transform.position);
coinCount += 1;
if(coinCount == 3)
{
SceneManager.LoadScene("GameClear");
}
}
}
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.CompareTag("Floor"))
{
isJumping = false;
}
}
}
![2501895d dcdd 48b4 be6d dc9e577cf196](https://codegenius.org/uploads/slide/image/5799/2501895d-dcdd-48b4-be6d-dc9e577cf196.jpeg)
コインアイコンの表示
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameController : MonoBehaviour
{
public Text timeLabel;
public float timeCount;
// ★追加(コインアイコン)
// 配列(複数のデータを入れることのできる仕切りのある箱)
public GameObject[] coinIcons;
private Ball ballScript;
private int getCoin;
void Start()
{
timeLabel.text = "TIME;" + timeCount;
// ★追加(コインアイコン)
// 「Ball」オブジェクトに付いている「Ball」スクリプトの情報を取得する。
ballScript = GameObject.Find("Ball").GetComponent<Ball>();
}
void Update()
{
timeCount -= Time.deltaTime;
timeLabel.text = "TIME;" + timeCount.ToString("0");
if (timeCount < 0)
{
SceneManager.LoadScene("GameOver");
}
// ★追加(コインアイコン)
// 「Ball」スクリプトの中にある「coinCount」の情報を取得する。
getCoin = ballScript.coinCount;
// for文(繰り返し文)
for (int i = 0; i < coinIcons.Length; i++)
{
if(getCoin <= i)
{
// コインアイコンを表示状態にする。
coinIcons[i].SetActive(true);
}
else
{
// コインアイコンを非表示状態にする。
coinIcons[i].SetActive(false);
}
}
}
}
![0c8dbf3a 8c8e 4974 bb3c f7b437be1f1b](https://codegenius.org/uploads/slide/image/5800/0c8dbf3a-8c8e-4974-bb3c-f7b437be1f1b.jpeg)
![0af5478a 75e4 480a 926a 530a25801871](https://codegenius.org/uploads/slide/image/5801/0af5478a-75e4-480a-926a-530a25801871.jpeg)
![C982c13f 2d78 4f68 b130 e0a2eb85bcb3](https://codegenius.org/uploads/slide/image/5802/c982c13f-2d78-4f68-b130-e0a2eb85bcb3.jpeg)
【2020版】BallGame(全27回)
他のコースを見る![9c0e0179 81d0 47b7 9421 6946693413fe](https://codegenius.org/uploads/slide/image/5786/9c0e0179-81d0-47b7-9421-6946693413fe.jpeg)
![7b27817d 49a1 41e6 9ece ec300efb2d5f](https://codegenius.org/uploads/slide/image/5787/7b27817d-49a1-41e6-9ece-ec300efb2d5f.jpeg)
![9997f76f b0c3 4f68 8414 ed325076b110](https://codegenius.org/uploads/slide/image/5788/9997f76f-b0c3-4f68-8414-ed325076b110.jpeg)
![0d0d7024 2654 490a ba8e e59bc72cb4b4](https://codegenius.org/uploads/slide/image/5789/0d0d7024-2654-490a-ba8e-e59bc72cb4b4.jpeg)
![69a50f48 36b2 45ea 9a83 b9133131c0e4](https://codegenius.org/uploads/slide/image/5790/69a50f48-36b2-45ea-9a83-b9133131c0e4.jpeg)
![Af5a4829 6d34 4201 abe0 30606d9e3ab5](https://codegenius.org/uploads/slide/image/5791/af5a4829-6d34-4201-abe0-30606d9e3ab5.jpeg)
![B3575b8f 89cb 4618 b28f e896c54b334e](https://codegenius.org/uploads/slide/image/5792/b3575b8f-89cb-4618-b28f-e896c54b334e.jpeg)
![Cb853347 6ff4 4b9b b109 f93df6580aae](https://codegenius.org/uploads/slide/image/5793/cb853347-6ff4-4b9b-b109-f93df6580aae.jpeg)
![68345ab0 a029 47b4 b540 3b252ca24790](https://codegenius.org/uploads/slide/image/5794/68345ab0-a029-47b4-b540-3b252ca24790.jpeg)
![1e203cdf a601 48c5 af3a 31c351be31d3](https://codegenius.org/uploads/slide/image/5795/1e203cdf-a601-48c5-af3a-31c351be31d3.jpeg)
![1e870128 9df0 4302 bfd0 73f401642276](https://codegenius.org/uploads/slide/image/5796/1e870128-9df0-4302-bfd0-73f401642276.jpeg)
![38eabed8 e351 449d b3ae 60140d7f0706](https://codegenius.org/uploads/slide/image/5797/38eabed8-e351-449d-b3ae-60140d7f0706.jpeg)
![C44a074d d8bb 41b6 962b f2da7f5a9fd9](https://codegenius.org/uploads/slide/image/5798/c44a074d-d8bb-41b6-962b-f2da7f5a9fd9.jpeg)
コインアイコンを表示する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Ball : MonoBehaviour
{
public float moveSpeed;
private Rigidbody rb;
public AudioClip coinGet;
public float jumpSpeed;
private bool isJumping = false;
// ★修正(privateをpublicに変更する)
public int coinCount = 0;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
float moveH = Input.GetAxis("Horizontal");
float moveV = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveH, 0, moveV);
rb.AddForce(movement * moveSpeed);
if (Input.GetButtonDown("Jump") && isJumping == false)
{
rb.velocity = Vector3.up * jumpSpeed;
isJumping = true;
}
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Coin"))
{
Destroy(other.gameObject);
AudioSource.PlayClipAtPoint(coinGet, transform.position);
coinCount += 1;
if(coinCount == 3)
{
SceneManager.LoadScene("GameClear");
}
}
}
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.CompareTag("Floor"))
{
isJumping = false;
}
}
}
![2501895d dcdd 48b4 be6d dc9e577cf196](https://codegenius.org/uploads/slide/image/5799/2501895d-dcdd-48b4-be6d-dc9e577cf196.jpeg)
コインアイコンの表示
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameController : MonoBehaviour
{
public Text timeLabel;
public float timeCount;
// ★追加(コインアイコン)
// 配列(複数のデータを入れることのできる仕切りのある箱)
public GameObject[] coinIcons;
private Ball ballScript;
private int getCoin;
void Start()
{
timeLabel.text = "TIME;" + timeCount;
// ★追加(コインアイコン)
// 「Ball」オブジェクトに付いている「Ball」スクリプトの情報を取得する。
ballScript = GameObject.Find("Ball").GetComponent<Ball>();
}
void Update()
{
timeCount -= Time.deltaTime;
timeLabel.text = "TIME;" + timeCount.ToString("0");
if (timeCount < 0)
{
SceneManager.LoadScene("GameOver");
}
// ★追加(コインアイコン)
// 「Ball」スクリプトの中にある「coinCount」の情報を取得する。
getCoin = ballScript.coinCount;
// for文(繰り返し文)
for (int i = 0; i < coinIcons.Length; i++)
{
if(getCoin <= i)
{
// コインアイコンを表示状態にする。
coinIcons[i].SetActive(true);
}
else
{
// コインアイコンを非表示状態にする。
coinIcons[i].SetActive(false);
}
}
}
}
![0c8dbf3a 8c8e 4974 bb3c f7b437be1f1b](https://codegenius.org/uploads/slide/image/5800/0c8dbf3a-8c8e-4974-bb3c-f7b437be1f1b.jpeg)
![0af5478a 75e4 480a 926a 530a25801871](https://codegenius.org/uploads/slide/image/5801/0af5478a-75e4-480a-926a-530a25801871.jpeg)
![C982c13f 2d78 4f68 b130 e0a2eb85bcb3](https://codegenius.org/uploads/slide/image/5802/c982c13f-2d78-4f68-b130-e0a2eb85bcb3.jpeg)
コインの残り枚数を表示する