地雷を作成する
爆風の処理
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Mine : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip sound;
// 爆発の力
private float power = 10f;
// 爆発の力が及ぶ範囲(半径)
private float radius = 10f;
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Player")
{
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 0.5f);
AudioSource.PlayClipAtPoint(sound, transform.position);
// タンクのRigidbodyを取得
Rigidbody rb = collision.gameObject.GetComponent<Rigidbody>();
// タンクのRigidbodyに爆発の力を加える。
// ForceMode.VelocityChange・・・>質量を無視して、瞬間的に速度を変化させる。
rb.AddExplosionForce(power, transform.position, radius, 1.0f, ForceMode.VelocityChange);
// 地雷を破壊する。
Destroy(gameObject);
}
}
}
地雷のダメージの処理
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class TankHealth : MonoBehaviour
{
private int tankHP = 10;
public Text HPLabel;
public string sceneName;
void Start()
{
HPLabel.text = "" + tankHP;
}
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag == "Shell")
{
tankHP -= 1;
HPLabel.text = "" + tankHP;
if(tankHP < 1)
{
this.gameObject.SetActive(false);
SceneManager.LoadScene(sceneName);
}
}
// ★追加(地雷のダメージ)
if(collision.gameObject.tag == "Mine")
{
tankHP -= 3;
HPLabel.text = "" + tankHP;
if(tankHP < 1)
{
this.gameObject.SetActive(false);
SceneManager.LoadScene(sceneName);
}
}
}
}
爆風の処理
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Mine : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip sound;
// 爆発の力
private float power = 10f;
// 爆発の力が及ぶ範囲(半径)
private float radius = 10f;
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Player")
{
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 0.5f);
AudioSource.PlayClipAtPoint(sound, transform.position);
// タンクのRigidbodyを取得
Rigidbody rb = collision.gameObject.GetComponent<Rigidbody>();
// タンクのRigidbodyに爆発の力を加える。
// ForceMode.VelocityChange・・・>質量を無視して、瞬間的に速度を変化させる。
rb.AddExplosionForce(power, transform.position, radius, 1.0f, ForceMode.VelocityChange);
// 地雷を破壊する。
Destroy(gameObject);
}
}
}
地雷のダメージの処理
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class TankHealth : MonoBehaviour
{
private int tankHP = 10;
public Text HPLabel;
public string sceneName;
void Start()
{
HPLabel.text = "" + tankHP;
}
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag == "Shell")
{
tankHP -= 1;
HPLabel.text = "" + tankHP;
if(tankHP < 1)
{
this.gameObject.SetActive(false);
SceneManager.LoadScene(sceneName);
}
}
// ★追加(地雷のダメージ)
if(collision.gameObject.tag == "Mine")
{
tankHP -= 3;
HPLabel.text = "" + tankHP;
if(tankHP < 1)
{
this.gameObject.SetActive(false);
SceneManager.LoadScene(sceneName);
}
}
}
}
地雷を作成する