2017-02-21 121 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

减小碰撞标签下的半径比例。 您可以使用可视化模块内检查的边界进行可视化调试。