隠しオブジェクト(アイテム)の作り方
![6a454e66 6e22 48ec 9c7a ac0fc03157ad](https://codegenius.org/uploads/slide/image/9651/6a454e66-6e22-48ec-9c7a-ac0fc03157ad.jpeg)
![9e267adc 890b 4150 aff8 2555e3c178e2](https://codegenius.org/uploads/slide/image/9652/9e267adc-890b-4150-aff8-2555e3c178e2.jpeg)
![5ca24782 9034 4e72 8f93 53fd09e1dfe8](https://codegenius.org/uploads/slide/image/9653/5ca24782-9034-4e72-8f93-53fd09e1dfe8.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;
private Vector3 pos;
public GameObject[] coinIcons;
// ★追加
public GameObject key;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
pos = transform.position;
if (pos.y < -10)
{
SceneManager.LoadScene("GameOver");
}
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);
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;
}
}
}
![6363f719 0f85 4207 9d30 d31e92aaa329](https://codegenius.org/uploads/slide/image/9654/6363f719-0f85-4207-9d30-d31e92aaa329.jpeg)
![0b629a00 62ea 4b47 b964 a32bf75400b6](https://codegenius.org/uploads/slide/image/9655/0b629a00-62ea-4b47-b964-a32bf75400b6.jpeg)
![E38033b9 6174 41b2 ab6d 0b4834ebe0eb](https://codegenius.org/uploads/slide/image/9656/e38033b9-6174-41b2-ab6d-0b4834ebe0eb.jpeg)
【2022版】BallGame(全27回)
他のコースを見る![6a454e66 6e22 48ec 9c7a ac0fc03157ad](https://codegenius.org/uploads/slide/image/9651/6a454e66-6e22-48ec-9c7a-ac0fc03157ad.jpeg)
![9e267adc 890b 4150 aff8 2555e3c178e2](https://codegenius.org/uploads/slide/image/9652/9e267adc-890b-4150-aff8-2555e3c178e2.jpeg)
![5ca24782 9034 4e72 8f93 53fd09e1dfe8](https://codegenius.org/uploads/slide/image/9653/5ca24782-9034-4e72-8f93-53fd09e1dfe8.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;
private Vector3 pos;
public GameObject[] coinIcons;
// ★追加
public GameObject key;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
pos = transform.position;
if (pos.y < -10)
{
SceneManager.LoadScene("GameOver");
}
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);
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;
}
}
}
![6363f719 0f85 4207 9d30 d31e92aaa329](https://codegenius.org/uploads/slide/image/9654/6363f719-0f85-4207-9d30-d31e92aaa329.jpeg)
![0b629a00 62ea 4b47 b964 a32bf75400b6](https://codegenius.org/uploads/slide/image/9655/0b629a00-62ea-4b47-b964-a32bf75400b6.jpeg)
![E38033b9 6174 41b2 ab6d 0b4834ebe0eb](https://codegenius.org/uploads/slide/image/9656/e38033b9-6174-41b2-ab6d-0b4834ebe0eb.jpeg)
隠しオブジェクト(アイテム)の作り方