敵のコアの回転を数秒間だけ停止させる
![E4cc4455 7c54 4255 9af1 f8820e78f9c2](https://codegenius.org/uploads/slide/image/8279/e4cc4455-7c54-4255-9af1-f8820e78f9c2.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;
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;
}
}
![011b742e 0aa1 420e 91ba 5c1fa1c94e19](https://codegenius.org/uploads/slide/image/8280/011b742e-0aa1-420e-91ba-5c1fa1c94e19.jpeg)
回転の停止
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);
}
}
}
![722a4807 e403 48d7 b2ee 936cf51a4373](https://codegenius.org/uploads/slide/image/8281/722a4807-e403-48d7-b2ee-936cf51a4373.jpeg)
![1c2619af 97ff 40ce aa53 d805382ac678](https://codegenius.org/uploads/slide/image/8282/1c2619af-97ff-40ce-aa53-d805382ac678.jpeg)
【2021版】X_Mission(全34回)
他のコースを見る![E4cc4455 7c54 4255 9af1 f8820e78f9c2](https://codegenius.org/uploads/slide/image/8279/e4cc4455-7c54-4255-9af1-f8820e78f9c2.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;
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;
}
}
![011b742e 0aa1 420e 91ba 5c1fa1c94e19](https://codegenius.org/uploads/slide/image/8280/011b742e-0aa1-420e-91ba-5c1fa1c94e19.jpeg)
回転の停止
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);
}
}
}
![722a4807 e403 48d7 b2ee 936cf51a4373](https://codegenius.org/uploads/slide/image/8281/722a4807-e403-48d7-b2ee-936cf51a4373.jpeg)
![1c2619af 97ff 40ce aa53 d805382ac678](https://codegenius.org/uploads/slide/image/8282/1c2619af-97ff-40ce-aa53-d805382ac678.jpeg)
敵のコアの回転を数秒間だけ停止させる