ゲームクリアーシーンの作成/UIイメージの使い方
![93b2d6db 765c 4318 8ec4 e98ad535b333](https://codegenius.org/uploads/slide/image/1914/93b2d6db-765c-4318-8ec4-e98ad535b333.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 other)
{
if (other.gameObject.CompareTag("Floor"))
{
isJumping = false;
}
}
}
![51e3c2b2 5d15 450e 9d82 e85adf98d7de](https://codegenius.org/uploads/slide/image/1915/51e3c2b2-5d15-450e-9d82-e85adf98d7de.jpeg)
![Bc797ff8 ee46 4455 9569 443012788a8f](https://codegenius.org/uploads/slide/image/1916/bc797ff8-ee46-4455-9569-443012788a8f.jpeg)
![901c3023 d595 4f03 bfde eee3b2aef7c4](https://codegenius.org/uploads/slide/image/1917/901c3023-d595-4f03-bfde-eee3b2aef7c4.jpeg)
![De995907 1a73 40e5 a553 c3be9d95418b](https://codegenius.org/uploads/slide/image/1918/de995907-1a73-40e5-a553-c3be9d95418b.jpeg)
![D4a2f9a4 4181 4c06 b6be b767337341bc](https://codegenius.org/uploads/slide/image/1919/d4a2f9a4-4181-4c06-b6be-b767337341bc.jpeg)
![08b8c3e3 1b86 4dd5 9362 55ac14b8bf83](https://codegenius.org/uploads/slide/image/1920/08b8c3e3-1b86-4dd5-9362-55ac14b8bf83.jpeg)
【2018版】BallGame(全25回)
他のコースを見る![93b2d6db 765c 4318 8ec4 e98ad535b333](https://codegenius.org/uploads/slide/image/1914/93b2d6db-765c-4318-8ec4-e98ad535b333.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 other)
{
if (other.gameObject.CompareTag("Floor"))
{
isJumping = false;
}
}
}
![51e3c2b2 5d15 450e 9d82 e85adf98d7de](https://codegenius.org/uploads/slide/image/1915/51e3c2b2-5d15-450e-9d82-e85adf98d7de.jpeg)
![Bc797ff8 ee46 4455 9569 443012788a8f](https://codegenius.org/uploads/slide/image/1916/bc797ff8-ee46-4455-9569-443012788a8f.jpeg)
![901c3023 d595 4f03 bfde eee3b2aef7c4](https://codegenius.org/uploads/slide/image/1917/901c3023-d595-4f03-bfde-eee3b2aef7c4.jpeg)
![De995907 1a73 40e5 a553 c3be9d95418b](https://codegenius.org/uploads/slide/image/1918/de995907-1a73-40e5-a553-c3be9d95418b.jpeg)
![D4a2f9a4 4181 4c06 b6be b767337341bc](https://codegenius.org/uploads/slide/image/1919/d4a2f9a4-4181-4c06-b6be-b767337341bc.jpeg)
![08b8c3e3 1b86 4dd5 9362 55ac14b8bf83](https://codegenius.org/uploads/slide/image/1920/08b8c3e3-1b86-4dd5-9362-55ac14b8bf83.jpeg)
ゲームクリアーシーンの作成/UIイメージの使い方