敵の攻撃を停止させるアイテムの作成
![9c4138b2 59e3 4acb 86a0 da62de04c128](https://codegenius.org/uploads/slide/image/2343/9c4138b2-59e3-4acb-86a0-da62de04c128.jpeg)
![7d718ec6 474d 4599 b90b 780dd0cb2e30](https://codegenius.org/uploads/slide/image/2344/7d718ec6-474d-4599-b90b-780dd0cb2e30.jpeg)
![9de7b9bb ea8a 4723 935a a6a088ac3c38](https://codegenius.org/uploads/slide/image/2345/9de7b9bb-ea8a-4723-935a-a6a088ac3c38.jpeg)
敵の攻撃を停止させる
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyShotShell : MonoBehaviour
{
public GameObject enemyShellPrefab;
public float shotSpeed;
public AudioClip shotSound;
private int shotInterval;
// ★追加
public float stopTimer = 5.0f;
void Update()
{
shotInterval += 1;
// ★追加
stopTimer -= Time.deltaTime;
// ★追加
// タイマーが0未満になったら、0で止める。
if (stopTimer < 0)
{
stopTimer = 0;
}
// ★追加
print("攻撃開始まであと" + stopTimer + "秒");
// ★追加
if (shotInterval % 60 == 0 && stopTimer <= 0)
{
GameObject enemyShell = Instantiate(enemyShellPrefab, transform.position, Quaternion.identity);
Rigidbody enemyShellRb = enemyShell.GetComponent<Rigidbody>();
enemyShellRb.AddForce(transform.forward * shotSpeed);
AudioSource.PlayClipAtPoint(shotSound, transform.position);
Destroy(enemyShell, 3.0f);
}
}
// ★追加
// 敵の攻撃をストップさせるメソッド(Timerの時間を増加させることで攻撃の停止時間を伸ばす)
// (考え方)HPを増加させるアイテム等と同じ
// このアイテムを複数取得すると、それだけ攻撃停止時間も「加算」される。
public void AddStopTimer(float amount)
{
stopTimer += amount;
}
}
![Bbae3d84 5f95 44bb b5fa 31aff43f49e6](https://codegenius.org/uploads/slide/image/2346/bbae3d84-5f95-44bb-b5fa-31aff43f49e6.jpeg)
敵の攻撃を停止させるアイテム
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class StopAttackItem : MonoBehaviour
{
private GameObject[] targets;
public AudioClip getSound;
public GameObject effectPrefab;
void Update()
{
// 「EnemyShotShell」オブジェクトに「EnemyShotShell」タグを設定してください(ポイント)
targets = GameObject.FindGameObjectsWithTag("EnemyShotShell");
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
for (int i = 0; i < targets.Length; i++)
{
// 攻撃停止時間を3秒加算する。
targets[i].GetComponent<EnemyShotShell>().AddStopTimer(3.0f);
}
Destroy(gameObject);
AudioSource.PlayClipAtPoint(getSound, Camera.main.transform.position);
GameObject effect = (GameObject)Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 0.5f);
}
}
}
![6047b787 0159 4e77 a940 8b6240e4399f](https://codegenius.org/uploads/slide/image/2347/6047b787-0159-4e77-a940-8b6240e4399f.jpeg)
![9ff49076 b179 4c83 bf9a 366017462307](https://codegenius.org/uploads/slide/image/2348/9ff49076-b179-4c83-bf9a-366017462307.jpeg)
![Bf36ad3c 9c66 4965 bc90 670520453ac6](https://codegenius.org/uploads/slide/image/2349/bf36ad3c-9c66-4965-bc90-670520453ac6.jpeg)
BattleTank(基礎/全31回)
他のコースを見る![9c4138b2 59e3 4acb 86a0 da62de04c128](https://codegenius.org/uploads/slide/image/2343/9c4138b2-59e3-4acb-86a0-da62de04c128.jpeg)
![7d718ec6 474d 4599 b90b 780dd0cb2e30](https://codegenius.org/uploads/slide/image/2344/7d718ec6-474d-4599-b90b-780dd0cb2e30.jpeg)
![9de7b9bb ea8a 4723 935a a6a088ac3c38](https://codegenius.org/uploads/slide/image/2345/9de7b9bb-ea8a-4723-935a-a6a088ac3c38.jpeg)
敵の攻撃を停止させる
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyShotShell : MonoBehaviour
{
public GameObject enemyShellPrefab;
public float shotSpeed;
public AudioClip shotSound;
private int shotInterval;
// ★追加
public float stopTimer = 5.0f;
void Update()
{
shotInterval += 1;
// ★追加
stopTimer -= Time.deltaTime;
// ★追加
// タイマーが0未満になったら、0で止める。
if (stopTimer < 0)
{
stopTimer = 0;
}
// ★追加
print("攻撃開始まであと" + stopTimer + "秒");
// ★追加
if (shotInterval % 60 == 0 && stopTimer <= 0)
{
GameObject enemyShell = Instantiate(enemyShellPrefab, transform.position, Quaternion.identity);
Rigidbody enemyShellRb = enemyShell.GetComponent<Rigidbody>();
enemyShellRb.AddForce(transform.forward * shotSpeed);
AudioSource.PlayClipAtPoint(shotSound, transform.position);
Destroy(enemyShell, 3.0f);
}
}
// ★追加
// 敵の攻撃をストップさせるメソッド(Timerの時間を増加させることで攻撃の停止時間を伸ばす)
// (考え方)HPを増加させるアイテム等と同じ
// このアイテムを複数取得すると、それだけ攻撃停止時間も「加算」される。
public void AddStopTimer(float amount)
{
stopTimer += amount;
}
}
![Bbae3d84 5f95 44bb b5fa 31aff43f49e6](https://codegenius.org/uploads/slide/image/2346/bbae3d84-5f95-44bb-b5fa-31aff43f49e6.jpeg)
敵の攻撃を停止させるアイテム
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class StopAttackItem : MonoBehaviour
{
private GameObject[] targets;
public AudioClip getSound;
public GameObject effectPrefab;
void Update()
{
// 「EnemyShotShell」オブジェクトに「EnemyShotShell」タグを設定してください(ポイント)
targets = GameObject.FindGameObjectsWithTag("EnemyShotShell");
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
for (int i = 0; i < targets.Length; i++)
{
// 攻撃停止時間を3秒加算する。
targets[i].GetComponent<EnemyShotShell>().AddStopTimer(3.0f);
}
Destroy(gameObject);
AudioSource.PlayClipAtPoint(getSound, Camera.main.transform.position);
GameObject effect = (GameObject)Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 0.5f);
}
}
}
![6047b787 0159 4e77 a940 8b6240e4399f](https://codegenius.org/uploads/slide/image/2347/6047b787-0159-4e77-a940-8b6240e4399f.jpeg)
![9ff49076 b179 4c83 bf9a 366017462307](https://codegenius.org/uploads/slide/image/2348/9ff49076-b179-4c83-bf9a-366017462307.jpeg)
![Bf36ad3c 9c66 4965 bc90 670520453ac6](https://codegenius.org/uploads/slide/image/2349/bf36ad3c-9c66-4965-bc90-670520453ac6.jpeg)
敵の攻撃を停止させるアイテムの作成