0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

unity用tntスクリプト

Posted at
using UnityEngine;
using System.Collections;

public class tntScript : MonoBehaviour
{
    private Renderer objectRenderer;
    private bool isEmissionOn = false;
    private Coroutine emissionCoroutine;
    private bool isColliding = false;
    public AudioClip explosionSound; // 爆発音のオーディオクリップ
    private AudioSource audioSource;

    void Start()
    {
        objectRenderer = GetComponent<Renderer>();
        audioSource = GetComponent<AudioSource>();
        DontDestroyOnLoad(gameObject); // オブジェクトをシーン間で保持する
    }

    void Update()
    {
        if (isColliding && Input.GetMouseButtonDown(0)) // 左クリック
        {
            if (emissionCoroutine == null)
            {
                Debug.Log("Emission toggle started.");
                emissionCoroutine = StartCoroutine(ToggleEmission());
            }
        }
    }

    void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.CompareTag("Player"))
        {
            isColliding = true;
            Debug.Log("Player collided.");
        }
    }

    void OnCollisionExit(Collision collision)
    {
        if (collision.gameObject.CompareTag("Player"))
        {
            isColliding = false;
            Debug.Log("Player exited collision.");
        }
    }

    IEnumerator ToggleEmission()
    {
        float elapsedTime = 0f;

        // 点滅が始まる瞬間に音を再生
        if (explosionSound != null && audioSource != null)
        {
            Debug.Log("Playing explosion sound...");
            audioSource.PlayOneShot(explosionSound);
        }
        else
        {
            if (explosionSound == null)
                Debug.LogError("Explosion sound is null!");
            if (audioSource == null)
                Debug.LogError("AudioSource is null!");
        }

        while (elapsedTime < 4f)
        {
            isEmissionOn = !isEmissionOn;
            SetEmission(isEmissionOn ? 10 : 0);
            Debug.Log("Emission set to " + (isEmissionOn ? 10 : 0));
            yield return new WaitForSeconds(0.5f); // 0.5秒ごとに切り替え
            elapsedTime += 0.5f;
        }
        //SetEmission(0); // 点滅終了後にEmissionをオフにする
        Debug.Log("Emission toggle ended.");
        StartCoroutine(Explosion()); // 4秒後にexplosion関数を呼び出す
    }

    void SetEmission(float intensity)
    {
        if (intensity != 0)
        {
            objectRenderer.material.EnableKeyword("_EMISSION");
            objectRenderer.material.SetColor("_EmissionColor", Color.white * intensity);
        }
        else
        {
            objectRenderer.material.DisableKeyword("_EMISSION");
        }
    }

    IEnumerator Explosion()
    {
        Debug.Log("Explosion triggered!");

        // オブジェクトを膨張させる
        Vector3 originalScale = transform.localScale;
        transform.localScale = originalScale * 1.5f;

        // プレーヤーを吹き飛ばす
        Collider[] colliders = Physics.OverlapSphere(transform.position, 10f); // 爆発範囲を設定
        foreach (Collider hit in colliders)
        {
            Rigidbody rb = hit.GetComponent<Rigidbody>();
            if (rb != null && hit.CompareTag("Player"))
            {
                rb.AddExplosionForce(1000f, transform.position, 10f, 1f, ForceMode.Impulse); // 吹き飛ばす力を設定
            }
        }

        yield return new WaitForSeconds(0.5f); // 0.5秒後に削除
        Destroy(transform.parent.gameObject); // 親オブジェクトを削除する
    }
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?