敵の攻撃を停止させるアイテムの作成
![5440180f 2db6 4520 ab19 a91269a93b64](https://codegenius.org/uploads/slide/image/4217/5440180f-2db6-4520-ab19-a91269a93b64.jpeg)
![497a3ab3 4c1b 4302 8912 de644d442b3a](https://codegenius.org/uploads/slide/image/4218/497a3ab3-4c1b-4302-8912-de644d442b3a.jpeg)
![543bf8ba f88d 4a11 9932 6a7a91a5c47d](https://codegenius.org/uploads/slide/image/4219/543bf8ba-f88d-4a11-9932-6a7a91a5c47d.jpeg)
敵の攻撃を停止させる
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyShotShell : MonoBehaviour
{
public float shotSpeed;
[SerializeField]
private GameObject enemyShellPrefab;
[SerializeField]
private AudioClip shotSound;
private int interval;
// ★追加
// 何秒間停止させるかは自由
public float stopTimer = 5.0f;
void Update()
{
interval += 1;
// ★追加
stopTimer -= Time.deltaTime;
// タイマーが0未満になったら、0で止める。
if (stopTimer < 0)
{
stopTimer = 0;
}
// ★追加
if (interval % 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;
}
}
![3c1e8bea c19a 4e0f b61a b1d18503ba74](https://codegenius.org/uploads/slide/image/4220/3c1e8bea-c19a-4e0f-b61a-b1d18503ba74.jpeg)
敵の攻撃を停止させるアイテム
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class StopAttackItem : MonoBehaviour
{
private GameObject[] targets;
[SerializeField]
private AudioClip getSound;
[SerializeField]
private 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, transform.position);
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 0.5f);
}
}
}
![E6feb0e2 2d09 4267 8cdf 6c92b8d789e0](https://codegenius.org/uploads/slide/image/4221/e6feb0e2-2d09-4267-8cdf-6c92b8d789e0.jpeg)
![E52c8318 e7d6 4232 b9d1 7bee64a78177](https://codegenius.org/uploads/slide/image/4222/e52c8318-e7d6-4232-b9d1-7bee64a78177.jpeg)
![E98c80e7 40c3 49c7 9240 6f486377de29](https://codegenius.org/uploads/slide/image/4223/e98c80e7-40c3-49c7-9240-6f486377de29.jpeg)
【2019版】BattleTank(基礎/全38回)
他のコースを見る![5440180f 2db6 4520 ab19 a91269a93b64](https://codegenius.org/uploads/slide/image/4217/5440180f-2db6-4520-ab19-a91269a93b64.jpeg)
![497a3ab3 4c1b 4302 8912 de644d442b3a](https://codegenius.org/uploads/slide/image/4218/497a3ab3-4c1b-4302-8912-de644d442b3a.jpeg)
![543bf8ba f88d 4a11 9932 6a7a91a5c47d](https://codegenius.org/uploads/slide/image/4219/543bf8ba-f88d-4a11-9932-6a7a91a5c47d.jpeg)
敵の攻撃を停止させる
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyShotShell : MonoBehaviour
{
public float shotSpeed;
[SerializeField]
private GameObject enemyShellPrefab;
[SerializeField]
private AudioClip shotSound;
private int interval;
// ★追加
// 何秒間停止させるかは自由
public float stopTimer = 5.0f;
void Update()
{
interval += 1;
// ★追加
stopTimer -= Time.deltaTime;
// タイマーが0未満になったら、0で止める。
if (stopTimer < 0)
{
stopTimer = 0;
}
// ★追加
if (interval % 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;
}
}
![3c1e8bea c19a 4e0f b61a b1d18503ba74](https://codegenius.org/uploads/slide/image/4220/3c1e8bea-c19a-4e0f-b61a-b1d18503ba74.jpeg)
敵の攻撃を停止させるアイテム
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class StopAttackItem : MonoBehaviour
{
private GameObject[] targets;
[SerializeField]
private AudioClip getSound;
[SerializeField]
private 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, transform.position);
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 0.5f);
}
}
}
![E6feb0e2 2d09 4267 8cdf 6c92b8d789e0](https://codegenius.org/uploads/slide/image/4221/e6feb0e2-2d09-4267-8cdf-6c92b8d789e0.jpeg)
![E52c8318 e7d6 4232 b9d1 7bee64a78177](https://codegenius.org/uploads/slide/image/4222/e52c8318-e7d6-4232-b9d1-7bee64a78177.jpeg)
![E98c80e7 40c3 49c7 9240 6f486377de29](https://codegenius.org/uploads/slide/image/4223/e98c80e7-40c3-49c7-9240-6f486377de29.jpeg)
敵の攻撃を停止させるアイテムの作成