facebook twitter hatena line email

Unity/Csharp/Particle

提供: 初心者エンジニアの簡易メモ
2021年6月2日 (水) 11:33時点におけるAdmin (トーク | 投稿記録)による版 (SpriteとParticleの順序)

移動: 案内検索

パーティクル再生

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

パーティクル停止

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

ホーミングミサイルを作る

追跡弾を2つ作ってみる例

  1. 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

ブラーとかは?参考になる? https://baba-s.hatenablog.com/entry/2018/04/10/094300

ホーミングレーザー

以下で青いレーザーが作れる

TrailRendererを使う。

参考:https://ukonn.net/unity-how-to-make-homing-laser

お金獲得イメージ

coneのパーティクルを使う。

    void AnimationMoney()
    {
        float sec = 2.5f;
        GameObject obj = GameObject.Find("MoneyParticleSystem");
        iTween.MoveTo(obj, iTween.Hash("easeType", iTween.EaseType.easeOutSine, "x", 0.7f, "y", 3.5f, "time", sec));

        Hashtable hash = new Hashtable(){
            {"from", 100f},
            {"to", 0f},
            {"time", sec},
            {"easeType",iTween.EaseType.easeOutCubic},
            {"onupdate", "OnUpdateValue"},
            {"onupdatetarget", gameObject},
        };
        iTween.ValueTo(gameObject, hash);
        ParticleSystem.MainModule particle = obj.GetComponent<ParticleSystem>().main;
        particle.maxParticles= Pref.GetStageSelectNowNum();
        Invoke("AnimationMoneyEnd", sec);
    }
    void OnUpdateValue(float value)
    {
        Debug.Log("value=" + value);
        GameObject obj = GameObject.Find("MoneyParticleSystem");
        if (obj != null)
        {
            var shape = obj.GetComponent<ParticleSystem>().shape;
            shape.angle = value / 4;
            float scale = 0.2f + value / 100;
            shape.scale = new Vector3(scale, scale, scale);
        }
    }
    void AnimationMoneyEnd()
    {
        GameObject obj = GameObject.Find("MoneyParticleSystem");
        Destroy(obj);
    }

オーラとか

http://ktk-kumamoto.hatenablog.com/entry/2014/09/28/080827

SpriteとParticleの順序

SpriteよりParticleを手前にしたいときは、 Renderer/OrderInLayerで順序などを1000とかにしてみる。

        Renderer particleSystemRenderer = obj.GetComponent<Renderer>();
        particleSystemRenderer.sortingOrder = 1000;

参考:http://tsubakit1.hateblo.jp/entry/2015/01/05/233000

unity公式パーティクル パック

https://assetstore.unity.com/packages/essentials/tutorial-projects/unity-particle-pack-127325?utm_source=youtube&utm_medium=social&utm_campaign=education_global_generalpromo_2018-09-04_particle-pack&utm_content=download_video