敵に発見されたら数秒後にゲームオーバーにする


ゲームオーバー
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering.PostProcessing;
// ★追加(ゲームオーバー)
using UnityEngine.SceneManagement;
public class EnemyCore : MonoBehaviour
{
public float lockRange;
public AudioClip alert;
private AudioSource audioS;
public Vector3 rot;
private PostProcessVolume processVolume;
public PostProcessProfile alertFile;
private void Start()
{
audioS = GetComponent<AudioSource>();
processVolume = GameObject.Find("PPManager").GetComponent<PostProcessVolume>();
}
void Update()
{
transform.Rotate(rot * Time.deltaTime);
RaycastHit hit;
Ray ray = new Ray(transform.position, transform.forward);
if(Physics.Raycast(ray, out hit, lockRange))
{
GameObject target = hit.collider.gameObject;
if(target.CompareTag("PlayerBody"))
{
GetComponent<MeshRenderer>().material.color = Color.red;
audioS.clip = alert;
audioS.Play();
processVolume.profile = alertFile;
// ★追加(ゲームオーバー)
// 2秒後にゲームオーバーにする。
Invoke("GoToGameOver", 2f);
}
}
}
// ★追加(ゲームオーバー)
void GoToGameOver()
{
SceneManager.LoadScene("GameOver");
}
}

【2021版】X_Mission(全34回)
他のコースを見る

ゲームオーバー
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering.PostProcessing;
// ★追加(ゲームオーバー)
using UnityEngine.SceneManagement;
public class EnemyCore : MonoBehaviour
{
public float lockRange;
public AudioClip alert;
private AudioSource audioS;
public Vector3 rot;
private PostProcessVolume processVolume;
public PostProcessProfile alertFile;
private void Start()
{
audioS = GetComponent<AudioSource>();
processVolume = GameObject.Find("PPManager").GetComponent<PostProcessVolume>();
}
void Update()
{
transform.Rotate(rot * Time.deltaTime);
RaycastHit hit;
Ray ray = new Ray(transform.position, transform.forward);
if(Physics.Raycast(ray, out hit, lockRange))
{
GameObject target = hit.collider.gameObject;
if(target.CompareTag("PlayerBody"))
{
GetComponent<MeshRenderer>().material.color = Color.red;
audioS.clip = alert;
audioS.Play();
processVolume.profile = alertFile;
// ★追加(ゲームオーバー)
// 2秒後にゲームオーバーにする。
Invoke("GoToGameOver", 2f);
}
}
}
// ★追加(ゲームオーバー)
void GoToGameOver()
{
SceneManager.LoadScene("GameOver");
}
}

敵に発見されたら数秒後にゲームオーバーにする