首页 文章

保持粒子系统变换和相对于其父级的锥半径

提问于
浏览
0

我有一个“升降机” . 在游戏中,你走进粒子系统并在空中移动(在y上) .

因此,粒子系统是立方体/升力的子元素 . 因此,在缩放立方体时,我不想更改粒子系统的设置 . 它应该自己扩展 .

当立方体在5上的y位置和在y的10上的高度/比例时,粒子系统应该将其自身置于底部 .

如您所见,我希望它是全自动的 .

因此,当我进入代码时,我得到了这个

[SerializeField]
ParticleSystem liftParticles;

private void Start()
{
    Vector3 objectScale = transform.localScale; // cube scaling

    Vector3 particlePos = liftParticles.transform.position; // temp position
    particlePos.y = (particlePos.y - objectScale.y) / 2; // move down on y
    liftParticles.transform.position = particlePos; // set particle position

    float transformScalingX = objectScale.x; // x scaling of the cube
    float transformScalingZ = objectScale.z; // z scaling of the cube

    var shape = liftParticles.shape; // set the cone radius now
    shape.radius = transformScalingX > transformScalingZ ? transformScalingX : transformScalingZ;
    liftParticles.shape = shape;
}

我想按照上面提到的以下示例进行..

立方体的缩放比例为(3,10,3),其位置为(0,5,0)

我当前的计算 particlePos.y 返回值-0,75 but ,它必须是-0,5 .

所以我的代码中有错误吗? (是的,我很明显...)

第二个问题是,如何更改粒子系统的半径?当试图参考锥体的半径时,它表示我无法改变它,它是只读的 .

是吗?我希望我能以某种方式改变这一点......

Edit:

显然,当缩放为(1,1,1)时,粒子系统必须总是在-0,5f上 . 不再需要计算了 .

但是我仍然需要改变形状的半径并设置粒子相对于升力高度的寿命 . 手段

private void Start()
{
    Vector3 liftScale = transform.localScale; // Liftscaling

    var shape = liftParticles.shape; // temp shape
    shape.radius = liftScale.x > liftScale.z ? liftScale.x : liftScale.z; // set radius
    liftParticles.shape = shape; // assign the temp shape to the real shape

    liftParticles.main.startLifetime = ; // set the liftetime of the particles relative to its parent height on y 
}

1 回答

  • 1

    据我所知,你让粒子系统成为升力机的一个子(一个立方体),所以它可以一起移动 . 如果您只是希望它们两者一起移动,但它们独立扩展,您应该考虑使用Empty GameObject作为父级 .

    您可以将此空游戏对象放置在多维数据集(您的电梯)的中间,然后使电梯和粒子过滤掉那个空游戏对象的子项 . 然后移动Empty GameObject而不是电梯,孩子们也会移动 .

    关于修改收音机,请尝试此脚本

    GameObject myParticleGenerator;
    
    ParticleSystem.ShapeModule pShape;
    
    pShape = yParticleGenerator.GetComponent<ParticleSystem>().shape;
    
    pShape.radius = 4.0f;
    

相关问题