//tips
//パーティクルに衝突判定をつける
パーティクル一粒一粒とコライダーの衝突を検知したときに呼び出されるOnCollisionEnterを利用する。
OnParticleCollision を使用する際、ParticleSystemの設定のCollision の Send Collision Messages にチェックをいれる。
Cubeに下記のスクリプトをつけてパーティクルと接触反応しているか確認する。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ParticleController : MonoBehaviour
{
private void OnParticleCollision(GameObject obj)
{
Debug.Log("何かと衝突しました");
if (obj.tag == "Enemy")
{
Debug.Log("敵と衝突しました");
}
}
}
パーティクルに以前のenemyのスクリプトをアタッチして動かす。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class EnemyController4 : MonoBehaviour
{
GameObject Player;
Vector3 MOVEX = new Vector3(1.0f, 0, 0);
Vector3 MOVEY = new Vector3(0, 1.0f, 0);
Vector3 MOVEZ = new Vector3(0, 0, 1.0f);
private GameObject mainCamera; //メインカメラ格納用
private GameObject subCamera; //サブカメラ格納用
Vector3 target; // 入力受付時、移動後の位置を算出して保存
private int count = 0;
void Start()
{
target = transform.position;
this.Player = GameObject.Find("Player");
mainCamera = GameObject.Find("MainCamera");
subCamera = GameObject.Find("SubCamera");
}
void Update()
{
if (Input.anyKeyDown)
{
count++;
if (count == 1)
{
target = transform.position - MOVEX;
}
if (count == 2)
{
target = transform.position - MOVEX;
}
if (count == 3)
{
target = transform.position + MOVEX;
}
if (count == 4)
{
target = transform.position + MOVEX;
count = 0;
}
}
Move();
Vector3 p1 = transform.position;
Vector3 p2 = this.Player.transform.position;
if (p1 == p2)
{
if (Player != null)
{
//Camera.main.transform.SetParent(null);
//subCamera.transform.SetParent(null);
Player.transform.DetachChildren();
//this.Player.SetActive(false);
Destroy(Player);
Invoke("Over", 0);
}
}
}
void Move()
{
this.transform.position = target;
}
void Over()
{
SceneManager.LoadScene("GameOver");
}
}
https://qrunch.net/@horukasu/entries/4XkZ79RisAae5be2
https://qiita.com/Mikoshi/items/ed2e02acdce3ee5c3998
//特定タイミングでのみパーティクルが発動する
まずparticle systemのplay on awakeのチェックを外し、クリックしたらパーティクルが発動するようにする。
スクリプトはパーティクル自身に付加する。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ParticleController : MonoBehaviour
{
private ParticleSystem particle;
void Start()
{
particle = this.GetComponent<ParticleSystem>();
}
void Update()
{
if (Input.anyKeyDown)
{
particle.Play();
Debug.Log("Hit");
}
}
}
次はエネミーの行動タイミングにパーティクルの起動を割り込ませる。
GameObject Flame;
private ParticleSystem particle;
Void Startには下記を追加した。
this.Flame = GameObject.Find("Flame");
particle = this.Flame.GetComponent<ParticleSystem>();
編集したスクリプトを下記に記載する。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class EnemyP : MonoBehaviour
{
GameObject Player;
Vector3 MOVEX = new Vector3(1.0f, 0, 0);
Vector3 MOVEY = new Vector3(0, 1.0f, 0);
Vector3 MOVEZ = new Vector3(0, 0, 1.0f);
private GameObject mainCamera; //メインカメラ格納用
private GameObject subCamera; //サブカメラ格納用
GameObject Flame;
private ParticleSystem particle;
Vector3 target; // 入力受付時、移動後の位置を算出して保存
private int count = 0;
void Start()
{
target = transform.position;
this.Player = GameObject.Find("Player");
mainCamera = GameObject.Find("MainCamera");
subCamera = GameObject.Find("SubCamera");
this.Flame = GameObject.Find("Flame");
particle = this.Flame.GetComponent<ParticleSystem>();
}
void Update()
{
if (Input.anyKeyDown)
{
count++;
if (count == 1)
{
target = transform.position - MOVEX;
}
if (count == 2)
{
target = transform.position - MOVEX;
particle.Play();
}
if (count == 3)
{
target = transform.position + MOVEX;
particle.Stop();
}
if (count == 4)
{
target = transform.position + MOVEX;
count = 0;
}
}
Move();
Vector3 p1 = transform.position;
Vector3 p2 = this.Player.transform.position;
if (p1 == p2)
{
if (Player != null)
{
//Camera.main.transform.SetParent(null);
//subCamera.transform.SetParent(null);
Player.transform.DetachChildren();
//this.Player.SetActive(false);
Destroy(Player);
Invoke("Over", 0);
}
}
}
void Move()
{
this.transform.position = target;
}
void Over()
{
SceneManager.LoadScene("GameOver");
}
}
BloodSprayであれば下記のスクリプトで接触反応があり、Gameoverシーンに移行するがflameのエフェクトに接触しても、接触反応が生じない。
これはflameが同じ座標から動かないことが原因のようだったので、animationを付与したが、それでも難しかった。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class FlameP : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
private void OnParticleCollision(GameObject obj)
{
Debug.Log("何かと衝突しました");
if (obj.tag == "Player")
{
Debug.Log("敵と衝突しました");
SceneManager.LoadScene("GameOver");
}
}
}
そのため、enemyのスクリプトを改変し、それをflame particleに付加することで炎が実体化しているときに判定が生じるようにする。
スクリプトでは座標がプレイヤーと同じでかつflameの行動カウントが2の時に判定が生じるものとすることで意図したものができた。それ以外のcountの時には危害を加えていない。
p1 == p2&&count==2の部分となる。
下記スクリプト。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class EnemyP2 : MonoBehaviour
{
GameObject Player;
Vector3 MOVEX = new Vector3(1.0f, 0, 0);
Vector3 MOVEY = new Vector3(0, 1.0f, 0);
Vector3 MOVEZ = new Vector3(0, 0, 1.0f);
GameObject Flame;
private ParticleSystem particle;
Vector3 target; // 入力受付時、移動後の位置を算出して保存
private int count = 0;
void Start()
{
target = transform.position;
this.Player = GameObject.Find("Player");
this.Flame = GameObject.Find("Flame");
particle = this.Flame.GetComponent<ParticleSystem>();
}
void Update()
{
if (Input.anyKeyDown)
{
count++;
if (count == 1)
{
target = transform.position - MOVEX;
}
if (count == 2)
{
target = transform.position - MOVEX;
particle.Play();
}
if (count == 3)
{
target = transform.position + MOVEX;
particle.Stop();
}
if (count == 4)
{
target = transform.position + MOVEX;
count = 0;
}
}
Move();
Vector3 p1 = transform.position;
Vector3 p2 = this.Player.transform.position;
if (p1 == p2&&count==2)
{
if (Player != null)
{
//Camera.main.transform.SetParent(null);
//subCamera.transform.SetParent(null);
Player.transform.DetachChildren();
//this.Player.SetActive(false);
Destroy(Player);
Invoke("Over", 0);
}
}
}
void Move()
{
this.transform.position = target;
}
void Over()
{
SceneManager.LoadScene("GameOver");
}
}