Unity实例化命令无法正常工作 [英] Unity Instantiate command not working properly

查看:108
本文介绍了Unity实例化命令无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我尝试在Unity 3D中实例化粒子系统时,该命令都会定位粒子,但不会播放/运行动画.

这是我的代码

  GameObject impactGO =实例化(impactEffect,hit.point,Quaternion.LookRotation(hit.normal));销毁(impactGO,sleep_); 

统一版本2021.1.1f1

解决方案

放置粒子,但不播放/运行动画

如果成功通过代码实例化了粒子系统,这似乎可以做到.有多种方法可以确保它在创建时启动.


唤醒时播放:

然后,您需要确保粒子系统具有


ParticleSystem.Play替代:

或者,您可以使用 ParticleSystem.Play 函数,可在创建后在代码中启动粒子系统.

  ParticleSystem impactPS = Instantiate(impactEffect,hit.point,Quaternion.LookRotation(hit.normal))作为ParticleSystem;impactPS.Play();销毁(impactPS.gameObjkect,sleep_); 


获取粒子系统的持续时间:

此外,如果要获取粒子系统的持续时间,可以使用 .main.duration .为了确保您在粒子系统完成播放之前不破坏 gameObject .

  float sleep_ = impactPS.main.duration; 

Whenever I try to Instantiate a particle system in Unity 3D the command positions the particles but doesn't play/runs the animation.

HEre's my code

GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(impactGO, sleep_);

Unity version-2021.1.1f1

解决方案

Positions the particles, but doesn't play/run the animation

If the particle system is instantiated by your code successful, which it seems to do. There are multiple ways to ensure it start when it's created.


Play on Awake:

Then you need to ensure that the particle system has Play On Awake set to enabled. As this will ensure the particle system starts playing as soon as you create it in your scene.


ParticleSystem.Play Altnertive:

Alternatively you can use the ParticleSystem.Play function to start the particle system in code after you've created it.

ParticleSystem impactPS = Instantiate(impactEffect, hit.point, 
    Quaternion.LookRotation(hit.normal)) as ParticleSystem;
impactPS.Play();
Destroy(impactPS.gameObjkect, sleep_);


Getting Duration of Particle System:

Additionally if you want to get the duration of your particle system you can use .main.duration. To ensure you don't destroy your gameObject before the particle system has finished playing.

float sleep_ = impactPS.main.duration;

这篇关于Unity实例化命令无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆