Implement a function to send hearts when you press the [Unity] button [Particle System]

1 minute read

Press the button once and you will get one heart.

[What you are doing]

・ Link the button and Particle System
-Play () Particle System every time OnButtonDown is detected

【environment】#

· Mac OSX El Capitan
・ Unity versiton: 2018.3.0

【result】#

https://youtu.be/K2ZmyMT1PFo

[How to make]

Final placement image
スクリーンショット 2020-09-07 22.36.49.png

① Create Particle System

・ Place Particle System in the scene
・ Uncheck Looping
・ Set the Start Speed value to 10.
・ Uncheck PlayOnAwake
・ Set Rate over Time of Emission to 0
・ Set Bursts of Emission as shown in the figure below.

  • Bursts can set how much particles are emitted at what timing.
    This time, I want one to appear the moment I press it, so Time = 0 and Count = 1.
    Screenshot 2020-09-07 22.01.03.png
    ・ Set Limit Velocity over Lifetime as shown in the figure below.
  • For the roles of Speed and Dampen, I referred to this site.
       http://tsubakit1.hateblo.jp/entry/2017/05/03/211922
    Screenshot 2020-09-07 22.04.19.png
    ・ Set Color over Lifetime as shown below.
    Screenshot 2020-09-07 22.05.24.png

② Make Material for Particle System

-Add a PNG image of this heart to the asset and change the Texture Type to Sprite (2D and UI)

  • I can’t see it because it’s pure white. ↓ There is an image of a heart here.
     Like.png

・ Create a new material
-Change Shader to Particles / Standard Unit
・ Set Render Mode to Fade
-Drag and drop the heart image to the Albedo □ of Material
-Put this Material in the Material in the Render of Particle System

③ Make a button that gives a heart when pressed

・ Place Button in the scene
-Attach FlashLike.cs

FlashLike.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FlashLike : MonoBehaviour
{
    public ParticleSystem likeEffect;
    // Start is called before the first frame update
    void Start()
    {
        likeEffect = likeEffect.GetComponent<ParticleSystem>();
    }

    public void PlayLikeEffect() {
        likeEffect.Play(); 
    }
}

-Substitute Particle Ststem from Inspector to LikeEffect of FlashLike component
-Select FlashLike, PlayLikeEffect from OnClick of Button component
Screenshot 2020-09-07 22.21.52.png

that’s all! Complete!
Play around with the Limit Velocity over Lifetime and Color Over Lifetime values to find out how to get your favorite heart!