ゲームオーバーの作成





下に落ちたらゲームオーバー
using UnityEngine;
using UnityEngine.SceneManagement;
public class Ball : MonoBehaviour
{
private InputSystem_Actions isa;
public float moveSpeed;
private Rigidbody rb;
public AudioClip coinSound;
public float jumpSpeed;
private bool isJumping = false;
private int coinCount = 0;
// ★追加
private Vector3 pos;
void Start()
{
isa = new InputSystem_Actions();
isa.Enable();
rb = GetComponent<Rigidbody>();
}
void Update()
{
// ★追加
// Ballの現在位置を取得する。
pos = transform.position;
if (pos.y < -10)
{
SceneManager.LoadScene("GameOver");
}
Vector2 movement2 = isa.Player.Move.ReadValue<Vector2>();
Vector3 movement3 = new Vector3(movement2.x, 0, movement2.y);
rb.AddForce(movement3 * moveSpeed);
if (isa.Player.Jump.triggered && isJumping == false)
{
rb.linearVelocity = Vector3.up * jumpSpeed;
isJumping = true;
}
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Coin"))
{
Destroy(other.gameObject);
AudioSource.PlayClipAtPoint(coinSound, transform.position);
coinCount += 1;
if (coinCount == 2)
{
SceneManager.LoadScene("GameClear");
}
}
}
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.CompareTag("Floor"))
{
isJumping = false;
}
}
}

【Unity6版】BallGame(全27回)
他のコースを見る




下に落ちたらゲームオーバー
using UnityEngine;
using UnityEngine.SceneManagement;
public class Ball : MonoBehaviour
{
private InputSystem_Actions isa;
public float moveSpeed;
private Rigidbody rb;
public AudioClip coinSound;
public float jumpSpeed;
private bool isJumping = false;
private int coinCount = 0;
// ★追加
private Vector3 pos;
void Start()
{
isa = new InputSystem_Actions();
isa.Enable();
rb = GetComponent<Rigidbody>();
}
void Update()
{
// ★追加
// Ballの現在位置を取得する。
pos = transform.position;
if (pos.y < -10)
{
SceneManager.LoadScene("GameOver");
}
Vector2 movement2 = isa.Player.Move.ReadValue<Vector2>();
Vector3 movement3 = new Vector3(movement2.x, 0, movement2.y);
rb.AddForce(movement3 * moveSpeed);
if (isa.Player.Jump.triggered && isJumping == false)
{
rb.linearVelocity = Vector3.up * jumpSpeed;
isJumping = true;
}
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Coin"))
{
Destroy(other.gameObject);
AudioSource.PlayClipAtPoint(coinSound, transform.position);
coinCount += 1;
if (coinCount == 2)
{
SceneManager.LoadScene("GameClear");
}
}
}
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.CompareTag("Floor"))
{
isJumping = false;
}
}
}

ゲームオーバーの作成