隠しオブジェクト(アイテム)の作り方
条件成就でオブジェクトをアクティブ状態にする
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;
// ★追加
public GameObject target;
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);
coinCount += 1;
// ★追加
if(coinCount == 2){
// (ポイント)
// SetActive()メソッドはオブジェクトをアクティブ/非アクティブ状態にすることができる。
target.SetActive(true);
}
if(coinCount == 4)
SceneManager.LoadScene("GameClear");
}
}
public int Coin(){
return coinCount;
}
void OnCollisionEnter(Collision other){
if(other.gameObject.CompareTag("Floor")){
isJumping = false;
}
}
}
【旧版】BallGame(全25回)
他のコースを見る条件成就でオブジェクトをアクティブ状態にする
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;
// ★追加
public GameObject target;
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);
coinCount += 1;
// ★追加
if(coinCount == 2){
// (ポイント)
// SetActive()メソッドはオブジェクトをアクティブ/非アクティブ状態にすることができる。
target.SetActive(true);
}
if(coinCount == 4)
SceneManager.LoadScene("GameClear");
}
}
public int Coin(){
return coinCount;
}
void OnCollisionEnter(Collision other){
if(other.gameObject.CompareTag("Floor")){
isJumping = false;
}
}
}
隠しオブジェクト(アイテム)の作り方