敵のコアの回転を数秒間だけ停止させる

回転の停止
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;
private void Start()
{
audioS = GetComponent<AudioSource>();
processVolume = GameObject.Find("PPManager").GetComponent<PostProcessVolume>();
}
void Update()
{
// ★追加(回転停止)
transform.Rotate(rot * Time.deltaTime * speed); // 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;
// 3秒後に回転再開
Invoke("Restart", 3f);
}
// ★追加(回転停止)
void Restart()
{
speed = 1;
}
}

回転の停止
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NormalBeam : BeamBase
{
// ★追加(回転停止)
public AudioClip stopSound;
public override void OnTriggerEnter(Collider other)
{
base.OnTriggerEnter(other);
if (other.attachedRigidbody)
{
other.attachedRigidbody.AddForce(transform.forward * 500);
AudioSource.PlayClipAtPoint(Sound, Camera.main.transform.position);
}
// ★追加(回転停止)
if(other.TryGetComponent(out EnemyCore ec))
{
ec.RotationStop();
AudioSource.PlayClipAtPoint(stopSound, 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;
private void Start()
{
audioS = GetComponent<AudioSource>();
processVolume = GameObject.Find("PPManager").GetComponent<PostProcessVolume>();
}
void Update()
{
// ★追加(回転停止)
transform.Rotate(rot * Time.deltaTime * speed); // 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;
// 3秒後に回転再開
Invoke("Restart", 3f);
}
// ★追加(回転停止)
void Restart()
{
speed = 1;
}
}

回転の停止
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NormalBeam : BeamBase
{
// ★追加(回転停止)
public AudioClip stopSound;
public override void OnTriggerEnter(Collider other)
{
base.OnTriggerEnter(other);
if (other.attachedRigidbody)
{
other.attachedRigidbody.AddForce(transform.forward * 500);
AudioSource.PlayClipAtPoint(Sound, Camera.main.transform.position);
}
// ★追加(回転停止)
if(other.TryGetComponent(out EnemyCore ec))
{
ec.RotationStop();
AudioSource.PlayClipAtPoint(stopSound, Camera.main.transform.position);
}
}
}


敵のコアの回転を数秒間だけ停止させる