敵のコアに物理的な衝撃を与えて敵を破壊する
敵を破壊する
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 int speed = 1;
// ★追加(破壊)
public GameObject effectPrefab;
public AudioClip destroySound;
private void Start()
{
audioS = GetComponent<AudioSource>();
processVolume = GameObject.Find("PPManager").GetComponent<PostProcessVolume>();
}
void Update()
{
transform.Rotate(rot * Time.deltaTime * speed);
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;
Invoke("GoToGameOver", 2f);
}
}
}
void GoToGameOver()
{
SceneManager.LoadScene("GameOver");
}
public void RotationStop()
{
speed = 0;
Invoke("Restart", 3f);
}
void Restart()
{
speed = 1;
}
// ★追加(破壊)
private void OnCollisionEnter(Collision collision)
{
Destroy(transform.root.gameObject); // 親を破壊する(ポイント)
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 1.0f);
AudioSource.PlayClipAtPoint(destroySound, Camera.main.transform.position);
}
}
【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 int speed = 1;
// ★追加(破壊)
public GameObject effectPrefab;
public AudioClip destroySound;
private void Start()
{
audioS = GetComponent<AudioSource>();
processVolume = GameObject.Find("PPManager").GetComponent<PostProcessVolume>();
}
void Update()
{
transform.Rotate(rot * Time.deltaTime * speed);
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;
Invoke("GoToGameOver", 2f);
}
}
}
void GoToGameOver()
{
SceneManager.LoadScene("GameOver");
}
public void RotationStop()
{
speed = 0;
Invoke("Restart", 3f);
}
void Restart()
{
speed = 1;
}
// ★追加(破壊)
private void OnCollisionEnter(Collision collision)
{
Destroy(transform.root.gameObject); // 親を破壊する(ポイント)
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 1.0f);
AudioSource.PlayClipAtPoint(destroySound, Camera.main.transform.position);
}
}
敵のコアに物理的な衝撃を与えて敵を破壊する