(2025年7月28日)
<4個のバリアを破壊したらボスにダメージが入るようになる>
・「DestroyBoss」スクリプトの改良
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyBoss : MonoBehaviour
{
public GameObject effectPrefab;
public GameObject effectPrefab2;
public int objectHP;
public GameObject itemPrefab;
public int scoreValue;
public int damage = 1;
private ScoreManager sm;
// ★変更はここだけ!(publicに変更)
public bool barrier = true;
private void Start()
{
sm = GameObject.Find("ScoreLabel").GetComponent<ScoreManager>();
}
private void OnTriggerEnter(Collider other)
{
if (barrier == true)
{
return;
}
if (other.CompareTag("Shell"))
{
objectHP -= damage;
if (objectHP > 0)
{
Destroy(other.gameObject);
GameObject effect = Instantiate(effectPrefab, other.transform.position, Quaternion.identity);
Destroy(effect, 2.0f);
}
else
{
sm.AddScore(scoreValue);
Destroy(other.gameObject);
GameObject effect2 = Instantiate(effectPrefab2, other.transform.position, Quaternion.identity);
Destroy(this.gameObject);
if (itemPrefab)
{
Vector3 pos = transform.position;
Instantiate(itemPrefab, new Vector3(pos.x, pos.y + 0.6f, pos.z), Quaternion.identity);
}
}
}
}
}
・新規にC#スクリプトの作成
・名前を「GameController」に変更
・下記のコードを書いてチェック
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameController : MonoBehaviour
{
private GameObject[] barriers;
public GameObject bossBariier;
public DestroyBoss destroyBoss;
public AudioClip sound;
private bool isOk = false;
void Update()
{
barriers = GameObject.FindGameObjectsWithTag("Barrier.");
if (barriers.Length == 0)
{
if(isOk)
{
return;
}
// ボスのバリアを破壊する
Destroy(bossBariier.gameObject);
// ボスにダメージが入るようにする
destroyBoss.barrier = false;
// バリアが壊れる音を出す
AudioSource.PlayClipAtPoint(sound, Camera.main.transform.position);
isOk = true;
}
}
}
・新規にCreate Emptyオブジェクトを作成
・名前を「GameController」に変更
・これにスクリプトを追加
・下記3つの設定を行う
・Boss Barrierの空欄にはBoss2の中にある「Barrier.」オブジェクトを追加
・Destroy Bossの空欄には「Boss2」オブジェクトを追加
・Soundの空欄には「バリアが壊れる音」を追加
・タグの設定
(2025年3月3日)
<Keyオブジェクトに触れると、5秒間だけ無限ジャンプが可能になる>
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Boll : 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;
public AudioClip keysound;
// ★追加(無限ジャンプ)
private bool isMugen = false;
void Start()
{
rb = GetComponent();
}
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;
// ★追加(無限ジャンプ)
if (isMugen == false)
{
isjumping = true;
}
}
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Coin"))
{
Destroy(other.gameObject);
AudioSource.PlayClipAtPoint(coinGet, Camera.main.transform.position);
coinCount += 1;
coinIcons[coinCount - 1].SetActive(false);
if (coinCount == 1)
{
key.SetActive(true);
AudioSource.PlayClipAtPoint(keysound, Camera.main.transform.position);
}
if (coinCount == 3)
{
SceneManager.LoadScene("GameClear");
}
}
//キーというタグに触れたら無限ジャンプできる
if (other.CompareTag("Key"))
{
// ★変更(無限ジャンプ)
Destroy(other.gameObject);
isMugen = true;
// ボールの色を黒に変える
this.gameObject.GetComponent<MeshRenderer>() .material.color = Color.black;
Invoke("ResetMugen", 5f); // 5秒間だけ無限ジャンプできる
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Floor"))
{
isjumping = false;
}
}
// ★追加(無限ジャンプ)
void ResetMugen()
{
isMugen = false;
// ボールの色を緑に戻す
this.gameObject.GetComponent<MeshRenderer>() .material.color = Color.green;
}
}