オプションアイテムの効果のリセット
Optionをリセットする
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OptionController : MonoBehaviour
{
public GameObject[] options;
private bool moveOK = false;
private Vector3 playerPos;
private List<Vector3> playerPosRecord = new List<Vector3>();
private int optionCount = 0;
void Start()
{
playerPos = transform.position;
for (int i = 0; i < 40; i++)
{
playerPosRecord.Add(playerPos);
}
foreach(GameObject g in options)
{
g.SetActive(false);
}
}
void Update()
{
playerPos = transform.position;
float h = Mathf.Abs(Input.GetAxis("Horizontal"));
float v = Mathf.Abs(Input.GetAxis("Vertical"));
if (h > 0.5f || v > 0.5f)
{
moveOK = true;
}
else
{
moveOK = false;
}
if(moveOK)
{
playerPosRecord.Insert(0, playerPos);
playerPosRecord.RemoveAt(playerPosRecord.Count - 1);
}
options[0].transform.position = playerPosRecord[9];
options[1].transform.position = playerPosRecord[19];
options[2].transform.position = playerPosRecord[29];
options[3].transform.position = playerPosRecord[39];
}
public void AddOptionCount()
{
optionCount += 1;
switch (optionCount)
{
case 1:
options[0].SetActive(true);
break;
case 2:
options[1].SetActive(true);
break;
case 3:
options[2].SetActive(true);
break;
case 4:
options[3].SetActive(true);
break;
}
}
// ★追加(リセット)
public void ResetOption()
{
foreach(GameObject g in options)
{
g.SetActive(false);
}
optionCount = 0;
}
}
リセットメソッドの実行
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.SceneManagement;
public class PlayerHealth : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip damageSound;
public AudioClip destroySound;
public int maxHP;
private int HP;
public Slider HPSlider;
public int playerCount;
public TextMeshProUGUI playerLabel;
void Start()
{
HP = maxHP;
HPSlider.maxValue = HP;
HPSlider.value = HP;
playerLabel.text = "" + playerCount;
}
private void OnTriggerEnter(Collider other)
{
if(other.CompareTag("EnemyMissile"))
{
HP -= 1;
AudioSource.PlayClipAtPoint(damageSound, Camera.main.transform.position);
Destroy(other.gameObject);
HPSlider.value = HP;
if(HP == 0)
{
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 1.0f);
AudioSource.PlayClipAtPoint(destroySound, Camera.main.transform.position);
this.gameObject.SetActive(false);
playerCount -= 1;
playerLabel.text = "" + playerCount;
// ★追加(Optionリセット)
GetComponent<OptionController>().ResetOption();
if (playerCount > 0)
{
Invoke("Retry", 1.0f);
}
else
{
SceneManager.LoadScene("GameOver");
}
}
}
}
void Retry()
{
this.gameObject.SetActive(true);
HP = maxHP;
HPSlider.maxValue = HP;
HPSlider.value = HP;
GetComponent<PlayerMovement>().ResetMoveSpeed();
GetComponentInChildren<FireMissile>().ResetShot();
}
}
【2021版】Danmaku(基礎/全55回)
他のコースを見るOptionをリセットする
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OptionController : MonoBehaviour
{
public GameObject[] options;
private bool moveOK = false;
private Vector3 playerPos;
private List<Vector3> playerPosRecord = new List<Vector3>();
private int optionCount = 0;
void Start()
{
playerPos = transform.position;
for (int i = 0; i < 40; i++)
{
playerPosRecord.Add(playerPos);
}
foreach(GameObject g in options)
{
g.SetActive(false);
}
}
void Update()
{
playerPos = transform.position;
float h = Mathf.Abs(Input.GetAxis("Horizontal"));
float v = Mathf.Abs(Input.GetAxis("Vertical"));
if (h > 0.5f || v > 0.5f)
{
moveOK = true;
}
else
{
moveOK = false;
}
if(moveOK)
{
playerPosRecord.Insert(0, playerPos);
playerPosRecord.RemoveAt(playerPosRecord.Count - 1);
}
options[0].transform.position = playerPosRecord[9];
options[1].transform.position = playerPosRecord[19];
options[2].transform.position = playerPosRecord[29];
options[3].transform.position = playerPosRecord[39];
}
public void AddOptionCount()
{
optionCount += 1;
switch (optionCount)
{
case 1:
options[0].SetActive(true);
break;
case 2:
options[1].SetActive(true);
break;
case 3:
options[2].SetActive(true);
break;
case 4:
options[3].SetActive(true);
break;
}
}
// ★追加(リセット)
public void ResetOption()
{
foreach(GameObject g in options)
{
g.SetActive(false);
}
optionCount = 0;
}
}
リセットメソッドの実行
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.SceneManagement;
public class PlayerHealth : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip damageSound;
public AudioClip destroySound;
public int maxHP;
private int HP;
public Slider HPSlider;
public int playerCount;
public TextMeshProUGUI playerLabel;
void Start()
{
HP = maxHP;
HPSlider.maxValue = HP;
HPSlider.value = HP;
playerLabel.text = "" + playerCount;
}
private void OnTriggerEnter(Collider other)
{
if(other.CompareTag("EnemyMissile"))
{
HP -= 1;
AudioSource.PlayClipAtPoint(damageSound, Camera.main.transform.position);
Destroy(other.gameObject);
HPSlider.value = HP;
if(HP == 0)
{
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 1.0f);
AudioSource.PlayClipAtPoint(destroySound, Camera.main.transform.position);
this.gameObject.SetActive(false);
playerCount -= 1;
playerLabel.text = "" + playerCount;
// ★追加(Optionリセット)
GetComponent<OptionController>().ResetOption();
if (playerCount > 0)
{
Invoke("Retry", 1.0f);
}
else
{
SceneManager.LoadScene("GameOver");
}
}
}
}
void Retry()
{
this.gameObject.SetActive(true);
HP = maxHP;
HPSlider.maxValue = HP;
HPSlider.value = HP;
GetComponent<PlayerMovement>().ResetMoveSpeed();
GetComponentInChildren<FireMissile>().ResetShot();
}
}
オプションアイテムの効果のリセット