前提・実現したいこと
UnityのチュートリアルのTankに回復アイテムを付けたいです。
触れたら最大値まで回復させたいです。
発生している問題・エラーメッセージ
アイテムに触れたらアイテムは消えるのですが、回復をTankのHPのUIに反映させることができません。
自分で追加したスクリプト using System.Collections; using System.Collections.Generic; using UnityEngine; public class HPItem : MonoBehaviour { void OnCollisionEnter(Collision collision) { if (collision.gameObject.tag == "Player") { Destroy(gameObject); } } }
チュートリアルのスクリプト using UnityEngine; using UnityEngine.UI; public class TankHealth : MonoBehaviour { public float m_StartingHealth = 100f; public Slider m_Slider; public Image m_FillImage; public Color m_FullHealthColor = Color.green; public Color m_ZeroHealthColor = Color.red; public GameObject m_ExplosionPrefab; private AudioSource m_ExplosionAudio; private ParticleSystem m_ExplosionParticles; public float m_CurrentHealth; private bool m_Dead; //戦闘不能 private void Awake() //ゲーム開始 { m_ExplosionParticles = Instantiate(m_ExplosionPrefab).GetComponent<ParticleSystem>(); m_ExplosionAudio = m_ExplosionParticles.GetComponent<AudioSource>(); m_ExplosionParticles.gameObject.SetActive(false); } private void OnEnable() // 生き返る時 { m_CurrentHealth = m_StartingHealth; //HPMaxにする m_Dead = false; //死んでない SetHealthUI(); } public void TakeDamage(float amount) { // Adjust the tank's current health, update the UI based on the new health and check whether or not the tank is dead. m_CurrentHealth -= amount; //←ここをm_CurrentHealth += amount;で追加したり SetHealthUI(); //UIの更新 if (m_CurrentHealth <= 0f && !m_Dead) //←ここでelseを追加したりしました { OnDeath(); } } private void SetHealthUI() //スライダーのリセット { // Adjust the value and colour of the slider. m_Slider.value = m_CurrentHealth; m_FillImage.color = Color.Lerp(m_ZeroHealthColor, m_FullHealthColor, m_CurrentHealth / m_StartingHealth); } private void OnDeath() //HPが0になった { // Play the effects for the death of the tank and deactivate it. m_Dead = true; m_ExplosionParticles.transform.position = transform.position; m_ExplosionParticles.gameObject.SetActive(true); m_ExplosionParticles.Play(); m_ExplosionAudio.Play(); gameObject.SetActive(false); } }
チュートリアルのスクリプト using UnityEngine; public class ShellExplosion : MonoBehaviour { public LayerMask m_TankMask; public ParticleSystem m_ExplosionParticles; public AudioSource m_ExplosionAudio; public float m_MaxDamage = 100f; public float m_ExplosionForce = 1000f; public float m_MaxLifeTime = 2f; public float m_ExplosionRadius = 5f; private void Start() { Destroy(gameObject, m_MaxLifeTime); } private void OnTriggerEnter(Collider other) { // Find all the tanks in an area around the shell and damage them. Collider[] colliders = Physics.OverlapSphere(transform.position, m_ExplosionRadius, m_TankMask); for (int i = 0; i < colliders.Length; i++) { Rigidbody targetRigidbody = colliders[i].GetComponent<Rigidbody>(); if (!targetRigidbody) continue; targetRigidbody.AddExplosionForce(m_ExplosionForce, transform.position, m_ExplosionRadius); TankHealth targetHealth = targetRigidbody.GetComponent<TankHealth>(); if (!targetHealth) continue; float damage = CalculateDamage(targetRigidbody.position); targetHealth.TakeDamage(damage); } m_ExplosionParticles.transform.parent = null; m_ExplosionParticles.Play(); m_ExplosionAudio.Play(); Destroy (m_ExplosionParticles.gameObject, m_ExplosionParticles.main.duration); Destroy (gameObject); } private float CalculateDamage(Vector3 targetPosition) { // Calculate the amount of damage a target should take based on it's position. Vector3 explosionToTarget = targetPosition - transform.position; float explosionDistance = explosionToTarget.magnitude; float relativeDistance = (m_ExplosionRadius - explosionDistance) / m_ExplosionRadius; float damage = relativeDistance * m_MaxDamage; damage = Mathf.Max(0f, damage); return damage; } }
試したこと
コメントにも書きましたがTakeDamageのm_CurrentHealth -= amount;を m_CurrentHealth += amount;でもう一つ定義したりifの後にelseをつけたりしましたが上手くいきませんでした。
よろしくお願いします。
回答2件
あなたの回答
tips
プレビュー