敵を破壊する

敵を破壊する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyCore : MonoBehaviour
{
public int coreHP;
public GameObject effectPrefab;
public AudioClip sound;
void Update()
{
if(coreHP < 1)
{
// rootを使うことで「親オブジェクト」を指定できる(重要)
Destroy(transform.root.gameObject);
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 0.5f);
AudioSource.PlayClipAtPoint(sound, transform.position);
}
}
}

敵の破壊
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(LineRenderer))]
[RequireComponent(typeof(AudioSource))]
public class RayShot : MonoBehaviour
{
public Transform gunPoint;
private float fireRate = 0.25f;
private float weaponRange = 50f;
private float hitForce = 300f;
private Camera FPSCamera;
private WaitForSeconds shotDuration = new WaitForSeconds(0.07f);
private AudioSource gunAudio;
private LineRenderer laserLine;
private float nextFire;
private enum Mode
{
Normal,
ZeroGravity,
Gravity
}
private Mode mode;
private int modeTotalNum;
private int num = 0;
private MeshRenderer mr;
public AudioClip[] shotSounds;
public Material[] materials;
public AudioClip changeSound;
public GameObject laserGun;
// ★追加(敵の破壊)
public AudioClip damageSound;
public AudioClip reflectSound;
void Start()
{
laserLine = GetComponent<LineRenderer>();
gunAudio = GetComponent<AudioSource>();
FPSCamera = GetComponent<Camera>();
laserLine.enabled = false;
modeTotalNum = System.Enum.GetValues(typeof(Mode)).Length;
mr = laserGun.GetComponent<MeshRenderer>();
}
void Update()
{
ChangeMode();
Debug.DrawRay(transform.position, transform.forward * 60, Color.red);
if (Input.GetMouseButtonDown(0) && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
StartCoroutine(ShotEffect());
Vector3 rayOrigin = FPSCamera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0));
RaycastHit hit;
laserLine.SetPosition(0, gunPoint.position);
if (Physics.Raycast(rayOrigin, FPSCamera.transform.forward, out hit, weaponRange))
{
laserLine.SetPosition(1, hit.point);
// ★追加(敵の破壊)
string hitTag = hit.transform.gameObject.tag;
if(hitTag == "EnemyCore")
{
// 敵のコアのHPを減少させる
hit.transform.gameObject.GetComponent<EnemyCore>().coreHP -= 1;
AudioSource.PlayClipAtPoint(damageSound, transform.position);
}
if(hitTag == "EnemyBody")
{
// 敵のボディを攻撃してもHPは減少できない。
AudioSource.PlayClipAtPoint(reflectSound, transform.position);
}
// 以上、ここまで
if (hit.rigidbody != null)
{
hit.rigidbody.AddForce(-hit.normal * hitForce);
switch(mode)
{
case Mode.ZeroGravity:
hit.rigidbody.useGravity = false;
break;
case Mode.Gravity:
hit.rigidbody.useGravity = true;
break;
}
}
}
else
{
laserLine.SetPosition(1, rayOrigin + (FPSCamera.transform.forward * weaponRange));
}
}
}
private IEnumerator ShotEffect()
{
gunAudio.Play();
laserLine.enabled = true;
yield return shotDuration;
laserLine.enabled = false;
}
void ChangeMode()
{
if(Input.GetKeyDown(KeyCode.E))
{
AudioSource.PlayClipAtPoint(changeSound, transform.position);
num = (num + 1) % modeTotalNum;
gunAudio.clip = shotSounds[num];
mr.material = materials[num];
switch(num)
{
case 0:
mode = Mode.Normal;
break;
case 1:
mode = Mode.ZeroGravity;
break;
case 2:
mode = Mode.Gravity;
break;
}
}
}
}



ロックオンの追加
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LockOn : MonoBehaviour
{
public float lockRange = 50f;
public Image aimImage;
private Camera FPSCamera;
private Color originalColor;
void Start()
{
FPSCamera = GetComponent<Camera>();
originalColor = aimImage.color;
}
void Update()
{
Vector3 rayOrigin = FPSCamera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0));
RaycastHit hit;
if(Physics.Raycast(rayOrigin, FPSCamera.transform.forward, out hit, lockRange))
{
string hitTag = hit.transform.gameObject.tag;
// ★追加
// (ポイント)「||」の意味を復習しよう!
if(hitTag == "Target" || hitTag == "EnemyCore")
{
aimImage.color = new Color(1.0f, 0f, 0f, 1.0f);
}
else
{
aimImage.color = originalColor;
}
}
}
}

【2019版】X_Mission(基礎/全51回)
他のコースを見る
敵を破壊する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyCore : MonoBehaviour
{
public int coreHP;
public GameObject effectPrefab;
public AudioClip sound;
void Update()
{
if(coreHP < 1)
{
// rootを使うことで「親オブジェクト」を指定できる(重要)
Destroy(transform.root.gameObject);
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 0.5f);
AudioSource.PlayClipAtPoint(sound, transform.position);
}
}
}

敵の破壊
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(LineRenderer))]
[RequireComponent(typeof(AudioSource))]
public class RayShot : MonoBehaviour
{
public Transform gunPoint;
private float fireRate = 0.25f;
private float weaponRange = 50f;
private float hitForce = 300f;
private Camera FPSCamera;
private WaitForSeconds shotDuration = new WaitForSeconds(0.07f);
private AudioSource gunAudio;
private LineRenderer laserLine;
private float nextFire;
private enum Mode
{
Normal,
ZeroGravity,
Gravity
}
private Mode mode;
private int modeTotalNum;
private int num = 0;
private MeshRenderer mr;
public AudioClip[] shotSounds;
public Material[] materials;
public AudioClip changeSound;
public GameObject laserGun;
// ★追加(敵の破壊)
public AudioClip damageSound;
public AudioClip reflectSound;
void Start()
{
laserLine = GetComponent<LineRenderer>();
gunAudio = GetComponent<AudioSource>();
FPSCamera = GetComponent<Camera>();
laserLine.enabled = false;
modeTotalNum = System.Enum.GetValues(typeof(Mode)).Length;
mr = laserGun.GetComponent<MeshRenderer>();
}
void Update()
{
ChangeMode();
Debug.DrawRay(transform.position, transform.forward * 60, Color.red);
if (Input.GetMouseButtonDown(0) && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
StartCoroutine(ShotEffect());
Vector3 rayOrigin = FPSCamera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0));
RaycastHit hit;
laserLine.SetPosition(0, gunPoint.position);
if (Physics.Raycast(rayOrigin, FPSCamera.transform.forward, out hit, weaponRange))
{
laserLine.SetPosition(1, hit.point);
// ★追加(敵の破壊)
string hitTag = hit.transform.gameObject.tag;
if(hitTag == "EnemyCore")
{
// 敵のコアのHPを減少させる
hit.transform.gameObject.GetComponent<EnemyCore>().coreHP -= 1;
AudioSource.PlayClipAtPoint(damageSound, transform.position);
}
if(hitTag == "EnemyBody")
{
// 敵のボディを攻撃してもHPは減少できない。
AudioSource.PlayClipAtPoint(reflectSound, transform.position);
}
// 以上、ここまで
if (hit.rigidbody != null)
{
hit.rigidbody.AddForce(-hit.normal * hitForce);
switch(mode)
{
case Mode.ZeroGravity:
hit.rigidbody.useGravity = false;
break;
case Mode.Gravity:
hit.rigidbody.useGravity = true;
break;
}
}
}
else
{
laserLine.SetPosition(1, rayOrigin + (FPSCamera.transform.forward * weaponRange));
}
}
}
private IEnumerator ShotEffect()
{
gunAudio.Play();
laserLine.enabled = true;
yield return shotDuration;
laserLine.enabled = false;
}
void ChangeMode()
{
if(Input.GetKeyDown(KeyCode.E))
{
AudioSource.PlayClipAtPoint(changeSound, transform.position);
num = (num + 1) % modeTotalNum;
gunAudio.clip = shotSounds[num];
mr.material = materials[num];
switch(num)
{
case 0:
mode = Mode.Normal;
break;
case 1:
mode = Mode.ZeroGravity;
break;
case 2:
mode = Mode.Gravity;
break;
}
}
}
}



ロックオンの追加
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LockOn : MonoBehaviour
{
public float lockRange = 50f;
public Image aimImage;
private Camera FPSCamera;
private Color originalColor;
void Start()
{
FPSCamera = GetComponent<Camera>();
originalColor = aimImage.color;
}
void Update()
{
Vector3 rayOrigin = FPSCamera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0));
RaycastHit hit;
if(Physics.Raycast(rayOrigin, FPSCamera.transform.forward, out hit, lockRange))
{
string hitTag = hit.transform.gameObject.tag;
// ★追加
// (ポイント)「||」の意味を復習しよう!
if(hitTag == "Target" || hitTag == "EnemyCore")
{
aimImage.color = new Color(1.0f, 0f, 0f, 1.0f);
}
else
{
aimImage.color = originalColor;
}
}
}
}

敵を破壊する