ゲームクリアーシーンの作成/UIイメージの使い方
![4338d194 68c5 4b79 ad20 0842cd10e5a2](https://codegenius.org/uploads/slide/image/474/4338d194-68c5-4b79-ad20-0842cd10e5a2.jpeg)
ゲームクリアーシーンに遷移させる
using UnityEngine;
using System.Collections;
// ★追加
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;
}
}
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");
}
}
void OnCollisionEnter(Collision other){
if(other.gameObject.CompareTag("Floor")){
isJumping = false;
}
}
}
![D909b69c a00a 4c45 93d2 725412e120c4](https://codegenius.org/uploads/slide/image/475/d909b69c-a00a-4c45-93d2-725412e120c4.jpeg)
![9644614b 4c28 4ba7 85ed f6338f62a8c6](https://codegenius.org/uploads/slide/image/476/9644614b-4c28-4ba7-85ed-f6338f62a8c6.jpeg)
![29f10bf6 f20f 427f a4e2 95659a91b22b](https://codegenius.org/uploads/slide/image/477/29f10bf6-f20f-427f-a4e2-95659a91b22b.jpeg)
![2dcbd6be 067d 45cf 8007 013c3800252c](https://codegenius.org/uploads/slide/image/478/2dcbd6be-067d-45cf-8007-013c3800252c.jpeg)
![8384d973 5cfa 4211 9bd6 8d05f26df083](https://codegenius.org/uploads/slide/image/479/8384d973-5cfa-4211-9bd6-8d05f26df083.jpeg)
![6e6ad6eb aef1 48da 8cd8 728fc0f1f0df](https://codegenius.org/uploads/slide/image/480/6e6ad6eb-aef1-48da-8cd8-728fc0f1f0df.jpeg)
【旧版】BallGame(全25回)
他のコースを見る![4338d194 68c5 4b79 ad20 0842cd10e5a2](https://codegenius.org/uploads/slide/image/474/4338d194-68c5-4b79-ad20-0842cd10e5a2.jpeg)
ゲームクリアーシーンに遷移させる
using UnityEngine;
using System.Collections;
// ★追加
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;
}
}
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");
}
}
void OnCollisionEnter(Collision other){
if(other.gameObject.CompareTag("Floor")){
isJumping = false;
}
}
}
![D909b69c a00a 4c45 93d2 725412e120c4](https://codegenius.org/uploads/slide/image/475/d909b69c-a00a-4c45-93d2-725412e120c4.jpeg)
![9644614b 4c28 4ba7 85ed f6338f62a8c6](https://codegenius.org/uploads/slide/image/476/9644614b-4c28-4ba7-85ed-f6338f62a8c6.jpeg)
![29f10bf6 f20f 427f a4e2 95659a91b22b](https://codegenius.org/uploads/slide/image/477/29f10bf6-f20f-427f-a4e2-95659a91b22b.jpeg)
![2dcbd6be 067d 45cf 8007 013c3800252c](https://codegenius.org/uploads/slide/image/478/2dcbd6be-067d-45cf-8007-013c3800252c.jpeg)
![8384d973 5cfa 4211 9bd6 8d05f26df083](https://codegenius.org/uploads/slide/image/479/8384d973-5cfa-4211-9bd6-8d05f26df083.jpeg)
![6e6ad6eb aef1 48da 8cd8 728fc0f1f0df](https://codegenius.org/uploads/slide/image/480/6e6ad6eb-aef1-48da-8cd8-728fc0f1f0df.jpeg)
ゲームクリアーシーンの作成/UIイメージの使い方