facebook twitter hatena line email

「Unity/Csharp/Particle」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(ページの作成:「==パーティクル再生== GameObject gameObj = GameObject.Find ("ParticleSystem1"); gameObj.GetComponent<ParticleSystem> ().Stop (); ==パーティクル停止== G...」)
 
(パーティクル停止)
行6: 行6:
 
  GameObject gameObj = GameObject.Find("ParticleSystem1");
 
  GameObject gameObj = GameObject.Find("ParticleSystem1");
 
  gameObj.GetComponent<ParticleSystem> ().Play ();
 
  gameObj.GetComponent<ParticleSystem> ().Play ();
 +
 +
==ホーミングレーザーを作る==
 +
追跡弾を作る
 +
 +
UI/Particle Systemを新規で作る。2つ作ってみる例として、
 +
 +
"Particle System"というのと"Particle System2"というのを作る。
 +
<pre>
 +
 +
public class HomingScene : MonoBehaviour
 +
{
 +
    ParticleSystem ps;
 +
    ParticleSystem ps2;
 +
    ParticleSystem.Particle[] m_Particles;
 +
    public float threshold = 1f;
 +
    public float intensity = 1f;
 +
    public Transform target;
 +
 +
    public float _speed = 0.01f;    // 1秒間に進む距離
 +
    public float _rotSpeed = 180.0f;  // 1秒間に回転する角度
 +
    void Start()
 +
    {
 +
        ps = GameObject.Find("Particle System").GetComponent<ParticleSystem>();
 +
        ps2 = GameObject.Find("Particle System2").GetComponent<ParticleSystem>();
 +
        target = GameObject.Find("CircleSprite").transform;
 +
    }
 +
    void Update()
 +
    {
 +
        ViewParticle(ps);
 +
        ViewParticle(ps2);
 +
    }
 +
    void ViewParticle(ParticleSystem ps)
 +
    {
 +
        m_Particles = new ParticleSystem.Particle[ps.main.maxParticles];
 +
        int numParticlesAlive = ps.GetParticles(m_Particles);
 +
        for (int i = 0; i < numParticlesAlive; i++)
 +
        {
 +
            var forward = ps.transform.TransformPoint(m_Particles[i].velocity);
 +
            var position = ps.transform.TransformPoint(m_Particles[i].position);
 +
            var direction = (target.TransformPoint(target.position) - position).normalized;
 +
            var period = 1 - (m_Particles[i].remainingLifetime / m_Particles[i].startLifetime);
 +
            m_Particles[i].velocity = ps.transform.InverseTransformPoint(forward + direction * period);
 +
        }
 +
        ps.SetParticles(m_Particles, numParticlesAlive);
 +
    }
 +
}
 +
</pre>
 +
参考:https://qiita.com/Shinoda_Naoki/items/c6fbfc85fe24b2fcd5a1

2020年6月3日 (水) 22:27時点における版

パーティクル再生

GameObject gameObj = GameObject.Find ("ParticleSystem1");
gameObj.GetComponent<ParticleSystem> ().Stop ();

パーティクル停止

GameObject gameObj = GameObject.Find("ParticleSystem1");
gameObj.GetComponent<ParticleSystem> ().Play ();

ホーミングレーザーを作る

追跡弾を作る

UI/Particle Systemを新規で作る。2つ作ってみる例として、

"Particle System"というのと"Particle System2"というのを作る。


public class HomingScene : MonoBehaviour
{
    ParticleSystem ps;
    ParticleSystem ps2;
    ParticleSystem.Particle[] m_Particles;
    public float threshold = 1f;
    public float intensity = 1f;
    public Transform target;

    public float _speed = 0.01f;    // 1秒間に進む距離
    public float _rotSpeed = 180.0f;  // 1秒間に回転する角度
    void Start()
    {
        ps = GameObject.Find("Particle System").GetComponent<ParticleSystem>();
        ps2 = GameObject.Find("Particle System2").GetComponent<ParticleSystem>();
        target = GameObject.Find("CircleSprite").transform;
    }
    void Update()
    {
        ViewParticle(ps);
        ViewParticle(ps2);
    }
    void ViewParticle(ParticleSystem ps)
    {
        m_Particles = new ParticleSystem.Particle[ps.main.maxParticles];
        int numParticlesAlive = ps.GetParticles(m_Particles);
        for (int i = 0; i < numParticlesAlive; i++)
        {
            var forward = ps.transform.TransformPoint(m_Particles[i].velocity);
            var position = ps.transform.TransformPoint(m_Particles[i].position);
            var direction = (target.TransformPoint(target.position) - position).normalized;
            var period = 1 - (m_Particles[i].remainingLifetime / m_Particles[i].startLifetime);
            m_Particles[i].velocity = ps.transform.InverseTransformPoint(forward + direction * period);
        }
        ps.SetParticles(m_Particles, numParticlesAlive);
    }
}

参考:https://qiita.com/Shinoda_Naoki/items/c6fbfc85fe24b2fcd5a1