私は2Dゲームのプロトタイプを作っています。それは、ユーザーがクリックした場所で爆発するミサイルを発射するボールで構成されています。ミサイルの爆発は、ボールを打ち、ボールに力を加える粒子を放出する。ここにはvideoがあります。Unity - トリッキーパーティクルコリジョン
Collisionモジュールをアクティブにして標準パーティクルシステムを使用しました。そして、このスクリプトを各爆発で作成されたパーティクルシステムに添付してください:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class particleInteraction : MonoBehaviour {
//PS Variables
ParticleSystem myPS;
public List<ParticleCollisionEvent> particleCollisions = new List<ParticleCollisionEvent>();
//Physics variables
public float effect;
// Use this for initialization
void Start() {
myPS = GetComponent<ParticleSystem>();
}
void OnParticleCollision (GameObject other)
{
//Checking if the hit object is indeed the ball
if (other.tag.Equals("Player"))
{
Rigidbody2D hitObject = other.GetComponent<Rigidbody2D>();
//Getting the number of particles hat hit the ball
int noOfCollisions = myPS.GetCollisionEvents(other, particleCollisions);
Vector3 particleDirection = new Vector2(0,0); //The overall velocity of all the particles that collided
//Iterating through all the collisions and adding their vectors
for (int i = 0; i < noOfCollisions; i++)
{
particleDirection += particleCollisions[i].velocity;
}
//Applying the resultant force
hitObject.AddForce(particleDirection.normalized * effect * noOfCollisions);
}
}
}
これはほとんどの場合問題あります。ミサイルは壁にぶつかったときに爆発するように設計されているので、ボールが壁にあるとき、壁に向けられたミサイルがボールを押し出すことを期待しています。しかし、ボールは単に次のフレームの壁から離れて動きます(ビデオで見ることができます)。私はこれがボールの衝突者の内部で起こっている粒子上の衝突者のためだと信じています。これにより、物理エンジンはすぐに次のシーンでボールを移動させます。 私はOnParticleTriggerを使ってみましたが、Unityがパーティクルトリガーの影響を受けたゲームオブジェクトに関する情報を提供しないので、ボールには影響しません。
私はこの仕事をする方法を見つけるのを助けることができますか? 私は、交差する衝突者によって引き起こされるぎこちない動きを避けたい、あるいはミサイル爆発を表現するより良い方法を使用したいかもしれません。