隠しオブジェクト(アイテム)の作り方
![99fbb819 6b2c 4a3f 9622 a2a6c712eb00](https://codegenius.org/uploads/slide/image/5814/99fbb819-6b2c-4a3f-9622-a2a6c712eb00.jpeg)
![2aff0fba eb43 44da 8c69 bbaed7e67ce5](https://codegenius.org/uploads/slide/image/5815/2aff0fba-eb43-44da-8c69-bbaed7e67ce5.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;
public int coinCount = 0;
// ★追加
public GameObject moveFloor;
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);
coinCount += 1;
// ★追加
if (coinCount == 2)
{
// (ポイント)
// SetActive()メソッドはオブジェクトをアクティブ/非アクティブ状態にすることができる。
moveFloor.SetActive(true);
}
if (coinCount == 3)
{
SceneManager.LoadScene("GameClear");
}
}
}
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.CompareTag("Floor"))
{
isJumping = false;
}
}
}
![A6a30b55 3776 467c b0b6 e3495d814ca2](https://codegenius.org/uploads/slide/image/5816/a6a30b55-3776-467c-b0b6-e3495d814ca2.jpeg)
![082fcd84 e2dc 44f5 a849 033359b9d81d](https://codegenius.org/uploads/slide/image/5817/082fcd84-e2dc-44f5-a849-033359b9d81d.jpeg)
![7edc8d20 e5a0 4319 b46f af62f624c231](https://codegenius.org/uploads/slide/image/5818/7edc8d20-e5a0-4319-b46f-af62f624c231.jpeg)
【2020版】BallGame(全27回)
他のコースを見る![99fbb819 6b2c 4a3f 9622 a2a6c712eb00](https://codegenius.org/uploads/slide/image/5814/99fbb819-6b2c-4a3f-9622-a2a6c712eb00.jpeg)
![2aff0fba eb43 44da 8c69 bbaed7e67ce5](https://codegenius.org/uploads/slide/image/5815/2aff0fba-eb43-44da-8c69-bbaed7e67ce5.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;
public int coinCount = 0;
// ★追加
public GameObject moveFloor;
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);
coinCount += 1;
// ★追加
if (coinCount == 2)
{
// (ポイント)
// SetActive()メソッドはオブジェクトをアクティブ/非アクティブ状態にすることができる。
moveFloor.SetActive(true);
}
if (coinCount == 3)
{
SceneManager.LoadScene("GameClear");
}
}
}
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.CompareTag("Floor"))
{
isJumping = false;
}
}
}
![A6a30b55 3776 467c b0b6 e3495d814ca2](https://codegenius.org/uploads/slide/image/5816/a6a30b55-3776-467c-b0b6-e3495d814ca2.jpeg)
![082fcd84 e2dc 44f5 a849 033359b9d81d](https://codegenius.org/uploads/slide/image/5817/082fcd84-e2dc-44f5-a849-033359b9d81d.jpeg)
![7edc8d20 e5a0 4319 b46f af62f624c231](https://codegenius.org/uploads/slide/image/5818/7edc8d20-e5a0-4319-b46f-af62f624c231.jpeg)
隠しオブジェクト(アイテム)の作り方