团结 - 棘手的粒子碰撞

时间:2017-02-21 17:27:20

标签: unity3d unity5 unity3d-2dtools

我正在制作2D游戏的原型。它由一个发射导弹的球组成,目的是在用户点击的地方爆炸。导弹的爆炸释放出粒子,击中球并对球施加力。这是video

我使用了标准粒子系统并激活了碰撞模块。然后将此脚本附加到每次爆炸时创建的粒子系统:

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没有提供关于粒子触发器影响的游戏对象的信息,所以我不能影响球。

有人可以帮我找到一种方法来完成这项工作吗? 我想避免交叉对撞机引起的不稳定运动,或者使用更好的方法来表达导弹爆炸。

1 个答案:

答案 0 :(得分:0)

缩小“碰撞”选项卡下的“半径比例”。 您可以使用可视化模块内部检查的边界来直观地调试它。