敵のコアに物理的な衝撃を与えて敵を破壊する
![693c4991 55dd 4f06 ac8d 5abc007f2e14](https://codegenius.org/uploads/slide/image/8283/693c4991-55dd-4f06-ac8d-5abc007f2e14.jpeg)
敵を破壊する
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);
}
}
![55b7e1c2 f3a0 4afd a41e 9837ec3d5c3b](https://codegenius.org/uploads/slide/image/8284/55b7e1c2-f3a0-4afd-a41e-9837ec3d5c3b.jpeg)
![4ff8342d fd4d 403a b6f5 60ed86992180](https://codegenius.org/uploads/slide/image/8285/4ff8342d-fd4d-403a-b6f5-60ed86992180.jpeg)
![Ff8184e3 b78a 49b6 b4ae 8fd950a6de9e](https://codegenius.org/uploads/slide/image/8286/ff8184e3-b78a-49b6-b4ae-8fd950a6de9e.jpeg)
【2021版】X_Mission(全34回)
他のコースを見る![693c4991 55dd 4f06 ac8d 5abc007f2e14](https://codegenius.org/uploads/slide/image/8283/693c4991-55dd-4f06-ac8d-5abc007f2e14.jpeg)
敵を破壊する
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);
}
}
![55b7e1c2 f3a0 4afd a41e 9837ec3d5c3b](https://codegenius.org/uploads/slide/image/8284/55b7e1c2-f3a0-4afd-a41e-9837ec3d5c3b.jpeg)
![4ff8342d fd4d 403a b6f5 60ed86992180](https://codegenius.org/uploads/slide/image/8285/4ff8342d-fd4d-403a-b6f5-60ed86992180.jpeg)
![Ff8184e3 b78a 49b6 b4ae 8fd950a6de9e](https://codegenius.org/uploads/slide/image/8286/ff8184e3-b78a-49b6-b4ae-8fd950a6de9e.jpeg)
敵のコアに物理的な衝撃を与えて敵を破壊する