アイテム効果のリセット
移動速度のリセット
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
private float moveSpeed = 0.1f;
private Vector3 pos;
void Update()
{
float moveH = Input.GetAxis("Horizontal") * moveSpeed;
float moveV = Input.GetAxis("Vertical") * moveSpeed;
transform.Translate(moveH, 0, moveV);
MoveClamp();
}
void MoveClamp()
{
pos = transform.position;
pos.x = Mathf.Clamp(pos.x, -10, 10);
pos.z = Mathf.Clamp(pos.z, -10, 10);
transform.position = pos;
}
public void AddMoveSpeed(float amount)
{
moveSpeed += amount;
if(moveSpeed > 0.4f)
{
moveSpeed = 0.4f;
}
}
// ★追加(移動速度のリセット)
public void ResetMoveSpeed()
{
moveSpeed = 0.1f;
}
}
ミサイル速度・連射能力のリセット
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FireMissile : MonoBehaviour
{
public GameObject missilePrefab;
private float speed = 300;
public AudioClip sound;
private int count;
private int shotInterval = 30;
void FixedUpdate()
{
if (Input.GetKey(KeyCode.Space))
{
count += 1;
if (count % shotInterval == 0)
{
GameObject missile = Instantiate(missilePrefab, transform.position, Quaternion.identity);
Rigidbody missileRb = missile.GetComponent<Rigidbody>();
missileRb.AddForce(transform.forward * speed);
AudioSource.PlayClipAtPoint(sound, Camera.main.transform.position);
Destroy(missile, 2.0f);
}
}
}
public void AddShotSpeed(int amount)
{
speed += amount;
if (speed > 1000)
{
speed = 1000;
}
}
public void ReduceInterval(int amount)
{
shotInterval -= amount;
if(shotInterval < 5)
{
shotInterval = 5;
}
}
// ★追加(ミサイル速度・連射能力のリセット)
public void ResetShot()
{
// 初期の値に戻す。
speed = 300;
shotInterval = 30;
}
}
リセットメソッドの実行
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;
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回)
他のコースを見る移動速度のリセット
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
private float moveSpeed = 0.1f;
private Vector3 pos;
void Update()
{
float moveH = Input.GetAxis("Horizontal") * moveSpeed;
float moveV = Input.GetAxis("Vertical") * moveSpeed;
transform.Translate(moveH, 0, moveV);
MoveClamp();
}
void MoveClamp()
{
pos = transform.position;
pos.x = Mathf.Clamp(pos.x, -10, 10);
pos.z = Mathf.Clamp(pos.z, -10, 10);
transform.position = pos;
}
public void AddMoveSpeed(float amount)
{
moveSpeed += amount;
if(moveSpeed > 0.4f)
{
moveSpeed = 0.4f;
}
}
// ★追加(移動速度のリセット)
public void ResetMoveSpeed()
{
moveSpeed = 0.1f;
}
}
ミサイル速度・連射能力のリセット
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FireMissile : MonoBehaviour
{
public GameObject missilePrefab;
private float speed = 300;
public AudioClip sound;
private int count;
private int shotInterval = 30;
void FixedUpdate()
{
if (Input.GetKey(KeyCode.Space))
{
count += 1;
if (count % shotInterval == 0)
{
GameObject missile = Instantiate(missilePrefab, transform.position, Quaternion.identity);
Rigidbody missileRb = missile.GetComponent<Rigidbody>();
missileRb.AddForce(transform.forward * speed);
AudioSource.PlayClipAtPoint(sound, Camera.main.transform.position);
Destroy(missile, 2.0f);
}
}
}
public void AddShotSpeed(int amount)
{
speed += amount;
if (speed > 1000)
{
speed = 1000;
}
}
public void ReduceInterval(int amount)
{
shotInterval -= amount;
if(shotInterval < 5)
{
shotInterval = 5;
}
}
// ★追加(ミサイル速度・連射能力のリセット)
public void ResetShot()
{
// 初期の値に戻す。
speed = 300;
shotInterval = 30;
}
}
リセットメソッドの実行
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;
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(); // 子オブジェクトのコンポーネントの取得方法(ポイント)
}
}
アイテム効果のリセット