【发布时间】:2017-02-21 17:27:20
【问题描述】:
我正在为我的 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 不提供有关受粒子触发器影响的游戏对象的信息,所以我无法影响球。
谁能帮我找到一种方法来完成这项工作? 我想避免相交对撞机造成的抖动,或者使用更好的方法来表达导弹爆炸。
【问题讨论】:
标签: unity3d unity5 unity3d-2dtools