ゲームクリアーの作成
![Ccf428e3 9af4 4e51 9937 e971a10cb7c3](https://codegenius.org/uploads/slide/image/9581/ccf428e3-9af4-4e51-9937-e971a10cb7c3.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;
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);
// ★追加
// コインを1枚取得するごとに「coinCount」を1ずつ増加させる。
coinCount += 1;
// もしも「coinCount」が2になったら(条件)
if (coinCount == 2)
{
// GameClearシーンに遷移する。
// 遷移させるシーンは「名前」で特定するので「一言一句」合致させること(ポイント)
SceneManager.LoadScene("GameClear");
}
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Floor"))
{
isJumping = false;
}
}
}
![Fc6c1b09 ff36 4906 8f47 a5f22c7f3839](https://codegenius.org/uploads/slide/image/9582/fc6c1b09-ff36-4906-8f47-a5f22c7f3839.jpeg)
![0b6b68d7 d5d0 43cc ab13 ff6d0fa62c3c](https://codegenius.org/uploads/slide/image/9583/0b6b68d7-d5d0-43cc-ab13-ff6d0fa62c3c.jpeg)
![79ec53d5 a80d 4ec7 bdba 154114359573](https://codegenius.org/uploads/slide/image/9584/79ec53d5-a80d-4ec7-bdba-154114359573.jpeg)
![3cdfa651 ef6b 4c7b 9b3a 9869bf103603](https://codegenius.org/uploads/slide/image/9585/3cdfa651-ef6b-4c7b-9b3a-9869bf103603.jpeg)
![81f9d3b1 52f1 431f 952f 744522e8f650](https://codegenius.org/uploads/slide/image/9586/81f9d3b1-52f1-431f-952f-744522e8f650.jpeg)
![657e9ec7 ea82 4eaa 82a2 3488519eb164](https://codegenius.org/uploads/slide/image/9587/657e9ec7-ea82-4eaa-82a2-3488519eb164.jpeg)
![D53813b0 09ee 44dd a699 573aa06203ff](https://codegenius.org/uploads/slide/image/9588/d53813b0-09ee-44dd-a699-573aa06203ff.jpeg)
【2022版】BallGame(全27回)
他のコースを見る![Ccf428e3 9af4 4e51 9937 e971a10cb7c3](https://codegenius.org/uploads/slide/image/9581/ccf428e3-9af4-4e51-9937-e971a10cb7c3.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;
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);
// ★追加
// コインを1枚取得するごとに「coinCount」を1ずつ増加させる。
coinCount += 1;
// もしも「coinCount」が2になったら(条件)
if (coinCount == 2)
{
// GameClearシーンに遷移する。
// 遷移させるシーンは「名前」で特定するので「一言一句」合致させること(ポイント)
SceneManager.LoadScene("GameClear");
}
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Floor"))
{
isJumping = false;
}
}
}
![Fc6c1b09 ff36 4906 8f47 a5f22c7f3839](https://codegenius.org/uploads/slide/image/9582/fc6c1b09-ff36-4906-8f47-a5f22c7f3839.jpeg)
![0b6b68d7 d5d0 43cc ab13 ff6d0fa62c3c](https://codegenius.org/uploads/slide/image/9583/0b6b68d7-d5d0-43cc-ab13-ff6d0fa62c3c.jpeg)
![79ec53d5 a80d 4ec7 bdba 154114359573](https://codegenius.org/uploads/slide/image/9584/79ec53d5-a80d-4ec7-bdba-154114359573.jpeg)
![3cdfa651 ef6b 4c7b 9b3a 9869bf103603](https://codegenius.org/uploads/slide/image/9585/3cdfa651-ef6b-4c7b-9b3a-9869bf103603.jpeg)
![81f9d3b1 52f1 431f 952f 744522e8f650](https://codegenius.org/uploads/slide/image/9586/81f9d3b1-52f1-431f-952f-744522e8f650.jpeg)
![657e9ec7 ea82 4eaa 82a2 3488519eb164](https://codegenius.org/uploads/slide/image/9587/657e9ec7-ea82-4eaa-82a2-3488519eb164.jpeg)
![D53813b0 09ee 44dd a699 573aa06203ff](https://codegenius.org/uploads/slide/image/9588/d53813b0-09ee-44dd-a699-573aa06203ff.jpeg)
ゲームクリアーの作成