コインの残り枚数を表示する
![324a6466 b5b6 4a9e 9134 517ed58a5263](https://codegenius.org/uploads/slide/image/9627/324a6466-b5b6-4a9e-9134-517ed58a5263.jpeg)
![21cf4ef4 5f4c 4beb ac28 269403f58a0b](https://codegenius.org/uploads/slide/image/9628/21cf4ef4-5f4c-4beb-ac28-269403f58a0b.jpeg)
![Ef90b4bb 2f49 4873 9cf6 b4927f2ca023](https://codegenius.org/uploads/slide/image/9629/ef90b4bb-2f49-4873-9cf6-b4927f2ca023.jpeg)
![0a162c9b 754c 4b2a a0aa a832c0c63b42](https://codegenius.org/uploads/slide/image/9630/0a162c9b-754c-4b2a-a0aa-a832c0c63b42.jpeg)
![487a16a3 849c 4bab b622 01e95aeaa146](https://codegenius.org/uploads/slide/image/9631/487a16a3-849c-4bab-b622-01e95aeaa146.jpeg)
![34daaf49 a01c 48ac a405 b060a707e6e4](https://codegenius.org/uploads/slide/image/9632/34daaf49-a01c-48ac-a405-b060a707e6e4.jpeg)
![Dccb903c dee4 494d 8b8b b3c0cf950605](https://codegenius.org/uploads/slide/image/9633/dccb903c-dee4-494d-8b8b-b3c0cf950605.jpeg)
![34a5a171 3687 4a71 bd90 1080820cc4aa](https://codegenius.org/uploads/slide/image/9634/34a5a171-3687-4a71-bd90-1080820cc4aa.jpeg)
![257df090 869a 43a5 ac40 e4ed5644d843](https://codegenius.org/uploads/slide/image/9635/257df090-869a-43a5-ac40-e4ed5644d843.jpeg)
![Cf990623 04b3 4b56 8d3b 274ce581add6](https://codegenius.org/uploads/slide/image/9636/cf990623-04b3-4b56-8d3b-274ce581add6.jpeg)
![782b33dc c47f 485e 920a bee0c72df4a9](https://codegenius.org/uploads/slide/image/9637/782b33dc-c47f-485e-920a-bee0c72df4a9.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 int coinCount = 0;
private Vector3 pos;
// ★追加
public GameObject[] coinIcons;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
pos = transform.position;
if (pos.y < -10)
{
SceneManager.LoadScene("GameOver");
}
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;
// ★追加
coinIcons[coinCount - 1].SetActive(false);
if (coinCount == 3)
{
SceneManager.LoadScene("GameClear");
}
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Floor"))
{
isJumping = false;
}
}
}
![4c98a728 cb0c 465e 8183 263bde2da6b3](https://codegenius.org/uploads/slide/image/9638/4c98a728-cb0c-465e-8183-263bde2da6b3.jpeg)
![B1e40ab7 1bc2 40f9 a2ef e5c067ef4aaf](https://codegenius.org/uploads/slide/image/9639/b1e40ab7-1bc2-40f9-a2ef-e5c067ef4aaf.jpeg)
【2022版】BallGame(全27回)
他のコースを見る![324a6466 b5b6 4a9e 9134 517ed58a5263](https://codegenius.org/uploads/slide/image/9627/324a6466-b5b6-4a9e-9134-517ed58a5263.jpeg)
![21cf4ef4 5f4c 4beb ac28 269403f58a0b](https://codegenius.org/uploads/slide/image/9628/21cf4ef4-5f4c-4beb-ac28-269403f58a0b.jpeg)
![Ef90b4bb 2f49 4873 9cf6 b4927f2ca023](https://codegenius.org/uploads/slide/image/9629/ef90b4bb-2f49-4873-9cf6-b4927f2ca023.jpeg)
![0a162c9b 754c 4b2a a0aa a832c0c63b42](https://codegenius.org/uploads/slide/image/9630/0a162c9b-754c-4b2a-a0aa-a832c0c63b42.jpeg)
![487a16a3 849c 4bab b622 01e95aeaa146](https://codegenius.org/uploads/slide/image/9631/487a16a3-849c-4bab-b622-01e95aeaa146.jpeg)
![34daaf49 a01c 48ac a405 b060a707e6e4](https://codegenius.org/uploads/slide/image/9632/34daaf49-a01c-48ac-a405-b060a707e6e4.jpeg)
![Dccb903c dee4 494d 8b8b b3c0cf950605](https://codegenius.org/uploads/slide/image/9633/dccb903c-dee4-494d-8b8b-b3c0cf950605.jpeg)
![34a5a171 3687 4a71 bd90 1080820cc4aa](https://codegenius.org/uploads/slide/image/9634/34a5a171-3687-4a71-bd90-1080820cc4aa.jpeg)
![257df090 869a 43a5 ac40 e4ed5644d843](https://codegenius.org/uploads/slide/image/9635/257df090-869a-43a5-ac40-e4ed5644d843.jpeg)
![Cf990623 04b3 4b56 8d3b 274ce581add6](https://codegenius.org/uploads/slide/image/9636/cf990623-04b3-4b56-8d3b-274ce581add6.jpeg)
![782b33dc c47f 485e 920a bee0c72df4a9](https://codegenius.org/uploads/slide/image/9637/782b33dc-c47f-485e-920a-bee0c72df4a9.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 int coinCount = 0;
private Vector3 pos;
// ★追加
public GameObject[] coinIcons;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
pos = transform.position;
if (pos.y < -10)
{
SceneManager.LoadScene("GameOver");
}
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;
// ★追加
coinIcons[coinCount - 1].SetActive(false);
if (coinCount == 3)
{
SceneManager.LoadScene("GameClear");
}
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Floor"))
{
isJumping = false;
}
}
}
![4c98a728 cb0c 465e 8183 263bde2da6b3](https://codegenius.org/uploads/slide/image/9638/4c98a728-cb0c-465e-8183-263bde2da6b3.jpeg)
![B1e40ab7 1bc2 40f9 a2ef e5c067ef4aaf](https://codegenius.org/uploads/slide/image/9639/b1e40ab7-1bc2-40f9-a2ef-e5c067ef4aaf.jpeg)
コインの残り枚数を表示する