ボス専用のコア作成
ボスコア
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BossCore : MonoBehaviour
{
public GameObject damageEffectPrefab;
private int hitCount;
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Drone"))
{
hitCount += 1;
Destroy(collision.gameObject);
GameObject effect = Instantiate(damageEffectPrefab, collision.transform.position, Quaternion.identity);
Destroy(effect, 0.5f);
}
}
}
ボスコアへの攻撃
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")
{
hit.transform.gameObject.GetComponent<EnemyCore>().coreHP -= 1;
AudioSource.PlayClipAtPoint(damageSound, transform.position);
}
// ★追加(ボスコアへの攻撃)
// ボスのコアを攻撃しても無効
// (ポイント)BossCoreのタグも忘れずに設定すること!!!
if (hitTag == "EnemyBody" || hitTag == "BossCore")
{
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" || hitTag == "BossCore")
{
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 BossCore : MonoBehaviour
{
public GameObject damageEffectPrefab;
private int hitCount;
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Drone"))
{
hitCount += 1;
Destroy(collision.gameObject);
GameObject effect = Instantiate(damageEffectPrefab, collision.transform.position, Quaternion.identity);
Destroy(effect, 0.5f);
}
}
}
ボスコアへの攻撃
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")
{
hit.transform.gameObject.GetComponent<EnemyCore>().coreHP -= 1;
AudioSource.PlayClipAtPoint(damageSound, transform.position);
}
// ★追加(ボスコアへの攻撃)
// ボスのコアを攻撃しても無効
// (ポイント)BossCoreのタグも忘れずに設定すること!!!
if (hitTag == "EnemyBody" || hitTag == "BossCore")
{
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" || hitTag == "BossCore")
{
aimImage.color = new Color(1.0f, 0f, 0f, 1.0f);
}
else
{
aimImage.color = originalColor;
}
}
}
}
ボス専用のコア作成