编辑
所以看来,如果我将这些粒子作为我创建的预制枪对象的子代,它将起作用。我只是不能简单地从存储预制件的文件夹中引用预制件。这有意义吗?我觉得我应该能够只引用它们而无需将它们放在对象上。
嗨,所以我有下面的代码来获取射线投射的影响。然后,我将获得该对象的标签并使用它来查找我要显示的粒子系统。
这是为了使子弹撞击金属,污垢或水之类的表面。
我将这些粒子效果作为预制件存储在我创建的枪端预制件内部。粒子全部存储在数组内部。我旋转通过比较对象标记与粒子系统名称的数组。如果找到一个,则将其放在光线投射的位置并播放。
if (Physics.Raycast(cam.transform.position, forwardVector, out hit, range))
{
string impactName = hit.transform.tag;
//Debug.Log("This is the impact tag: " + impactName);
foreach (ParticleSystem ps in impactParticles)
{
//Debug.Log("Is " + ps.transform.name.ToLower() + " equal to " + impactName.ToLower() + " == " + ps.transform.name.ToLower().Contains(impactName.ToLower()));
if (ps.transform.name.ToLower().Contains(impactName.ToLower()))
{
// This than transforms the position of the particle effect to that of the area that is shot.
ps.transform.position = hit.point + (hit.normal * 0.002f);
ps.transform.forward = hit.transform.forward;
ps.transform.up = hit.normal;
ps.Play();
}
}
此代码不会给我任何错误,只是不会显示粒子效果。我不知道这是我错过的事情还是我不知道的事情。还是Unity的新手。
在将其制成预制件之前,我会简单地将其拖到脚本中,并且粒子系统处于场景中,并且它将像这样工作。