ボスの攻撃を作るその5(攻守の切り替えをデザインする)
ボスの攻撃の停止
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BossShotBeam : MonoBehaviour
{
public GameObject bossBeamPrefab;
public AudioClip sound;
public float area;
public int shotInterval;
private int count;
private GameObject target;
// ★追加(攻撃の停止)
public int maxPower = 300;
private int shotPower;
void Start()
{
target = GameObject.Find("Player");
// ★追加(攻撃の停止)
shotPower = maxPower;
}
void Update()
{
if(Vector3.Distance(transform.position, target.transform.position) < area)
{
// ★追加(攻撃の停止)
if (shotPower < 0)
{
// 攻撃が停止して5秒後にパワーを全回復させる。
Invoke("PowerRecovery", 5.0f);
return;
}
transform.LookAt(target.transform);
count += 1;
// ★追加(攻撃の停止)
shotPower -= 1;
if (count % shotInterval == 0)
{
GameObject bossBeam = Instantiate(bossBeamPrefab, transform.position, transform.rotation);
Destroy(bossBeam, 5.0f);
AudioSource.PlayClipAtPoint(sound, transform.position);
}
}
}
// ★追加(攻撃の停止)
void PowerRecovery()
{
shotPower = maxPower;
}
}
【2019版】X_Mission(基礎/全51回)
他のコースを見るボスの攻撃の停止
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BossShotBeam : MonoBehaviour
{
public GameObject bossBeamPrefab;
public AudioClip sound;
public float area;
public int shotInterval;
private int count;
private GameObject target;
// ★追加(攻撃の停止)
public int maxPower = 300;
private int shotPower;
void Start()
{
target = GameObject.Find("Player");
// ★追加(攻撃の停止)
shotPower = maxPower;
}
void Update()
{
if(Vector3.Distance(transform.position, target.transform.position) < area)
{
// ★追加(攻撃の停止)
if (shotPower < 0)
{
// 攻撃が停止して5秒後にパワーを全回復させる。
Invoke("PowerRecovery", 5.0f);
return;
}
transform.LookAt(target.transform);
count += 1;
// ★追加(攻撃の停止)
shotPower -= 1;
if (count % shotInterval == 0)
{
GameObject bossBeam = Instantiate(bossBeamPrefab, transform.position, transform.rotation);
Destroy(bossBeam, 5.0f);
AudioSource.PlayClipAtPoint(sound, transform.position);
}
}
}
// ★追加(攻撃の停止)
void PowerRecovery()
{
shotPower = maxPower;
}
}
ボスの攻撃を作るその5(攻守の切り替えをデザインする)