ゲームクリアーの作成
![Dfcc9557 49b1 428e a1cc bb046a7eb159](https://codegenius.org/uploads/slide/image/5736/dfcc9557-49b1-428e-a1cc-bb046a7eb159.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;
}
}
}
![B1652d68 cdfa 49a5 9d0f 504f8a2a4a47](https://codegenius.org/uploads/slide/image/5737/b1652d68-cdfa-49a5-9d0f-504f8a2a4a47.jpeg)
![600add74 4dba 477f a435 53f38ecbd75f](https://codegenius.org/uploads/slide/image/5738/600add74-4dba-477f-a435-53f38ecbd75f.jpeg)
![8fe82a48 fe35 471c 859d 02a7e4233bae](https://codegenius.org/uploads/slide/image/5739/8fe82a48-fe35-471c-859d-02a7e4233bae.jpeg)
![56b39301 76a6 4fc9 a5c5 b39724b28ab3](https://codegenius.org/uploads/slide/image/5740/56b39301-76a6-4fc9-a5c5-b39724b28ab3.jpeg)
![25535632 dad2 4302 bcc0 1bccc5c68c2f](https://codegenius.org/uploads/slide/image/5741/25535632-dad2-4302-bcc0-1bccc5c68c2f.jpeg)
![6e953094 c10b 48f5 bbca 53abad44879a](https://codegenius.org/uploads/slide/image/5742/6e953094-c10b-48f5-bbca-53abad44879a.jpeg)
【2020版】BallGame(全27回)
他のコースを見る![Dfcc9557 49b1 428e a1cc bb046a7eb159](https://codegenius.org/uploads/slide/image/5736/dfcc9557-49b1-428e-a1cc-bb046a7eb159.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;
}
}
}
![B1652d68 cdfa 49a5 9d0f 504f8a2a4a47](https://codegenius.org/uploads/slide/image/5737/b1652d68-cdfa-49a5-9d0f-504f8a2a4a47.jpeg)
![600add74 4dba 477f a435 53f38ecbd75f](https://codegenius.org/uploads/slide/image/5738/600add74-4dba-477f-a435-53f38ecbd75f.jpeg)
![8fe82a48 fe35 471c 859d 02a7e4233bae](https://codegenius.org/uploads/slide/image/5739/8fe82a48-fe35-471c-859d-02a7e4233bae.jpeg)
![56b39301 76a6 4fc9 a5c5 b39724b28ab3](https://codegenius.org/uploads/slide/image/5740/56b39301-76a6-4fc9-a5c5-b39724b28ab3.jpeg)
![25535632 dad2 4302 bcc0 1bccc5c68c2f](https://codegenius.org/uploads/slide/image/5741/25535632-dad2-4302-bcc0-1bccc5c68c2f.jpeg)
![6e953094 c10b 48f5 bbca 53abad44879a](https://codegenius.org/uploads/slide/image/5742/6e953094-c10b-48f5-bbca-53abad44879a.jpeg)
ゲームクリアーの作成