2017-02-21 55 views
0

我正在爲我的2D遊戲製作原型。它由發射導彈的球組成,目的是在用戶點擊的地方發生爆炸。導彈的爆炸釋放出粒子,擊中球並對球施加力量。這是一個videoUnity - 棘手的粒子碰撞

我使用了激活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不會提供有關粒子觸發器中受影響的gameobject的信息,所以我不能影響球。

任何人都可以幫助我找到一種方法來使這項工作? 我想避免交叉對撞機造成的生澀運動,或者使用更好的方法表達導彈爆炸。

回答

0

減小碰撞標籤下的半徑比例。 您可以使用可視化模塊內檢查的邊界進行可視化調試。