(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;
}
}