隠しオブジェクト(アイテム)の作り方



隠しオブジェクトの出現
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;
public GameObject[] coinIcons;
// ★追加
public GameObject key;
void Start()
{
isa = new InputSystem_Actions();
isa.Enable();
rb = GetComponent<Rigidbody>();
}
void Update()
{
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;
coinIcons[coinCount - 1].SetActive(false);
// ★追加
if (coinCount == 1)
{
// (ポイント)アクティブ状態にする。
key.SetActive(true);
}
if (coinCount == 3)
{
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;
public GameObject[] coinIcons;
// ★追加
public GameObject key;
void Start()
{
isa = new InputSystem_Actions();
isa.Enable();
rb = GetComponent<Rigidbody>();
}
void Update()
{
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;
coinIcons[coinCount - 1].SetActive(false);
// ★追加
if (coinCount == 1)
{
// (ポイント)アクティブ状態にする。
key.SetActive(true);
}
if (coinCount == 3)
{
SceneManager.LoadScene("GameClear");
}
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Floor"))
{
isJumping = false;
}
}
}



隠しオブジェクト(アイテム)の作り方