敵に発見機能をもたせる
発見機能
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyCore : MonoBehaviour
{
public float lockRange;
public AudioClip alert;
private AudioSource audioS;
public Vector3 rot;
private void Start()
{
audioS = GetComponent<AudioSource>();
}
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();
}
}
}
}
【2021版】X_Mission(全34回)
他のコースを見る発見機能
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyCore : MonoBehaviour
{
public float lockRange;
public AudioClip alert;
private AudioSource audioS;
public Vector3 rot;
private void Start()
{
audioS = GetComponent<AudioSource>();
}
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();
}
}
}
}
敵に発見機能をもたせる