code ソーシャル

Unityアプリcodetips(37)

スポンサーリンク

//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");
}

 

}

人気の記事

1

皆さん、ついに、エアラインでも、サブスクリプションが始まったのはご存じですか? まだ実験段階ですが、ANAが、定額全国住み放題サービスを提供する「ADDress」と組んで、国内線を4回まで定額利用可能 ...

2

無料でネットショップを開けるアプリとして多くの人に驚きを与えたBASE株式会社が、2019年10月25日東証マザーズに上場しました。2020年2月時点で90万店を超えるショップを抱えるまでに成長してい ...

3

2011年にサービスを開始してから圧倒的な成長率を誇るインテリア通販サイト 【FLYMEe/フライミー】を皆さんご存じでしょうか。 「自分のイメージするインテリア、本当に欲しいインテリアがどこにあるの ...

4

ついに、noteの月間アクティブユーザー数が4400万人(2020年3月時点)に到達しました。 そもそも、「note」とは、クリエイターが、文章やマンガ、写真、音声を投稿することができ、ユーザーはその ...

5

ボードゲームカフェが1日2回転で儲かるという記事をみつけたので興味を持ち、調べてみました。 まずは、需要がどれくらいあるのか、市場のようすからみていきましょう。 世界最大のボードゲーム市場はドイツで、 ...

-code, ソーシャル
-,

Copyright© BUSINESS HUNTER , 2023 All Rights Reserved Powered by AFFINGER5.