Search Unity

[Unity2D] Effect on mouse click and holding mouse

Discussion in '2D' started by khanpham1995, Aug 17, 2018.

  1. khanpham1995

    khanpham1995

    Joined:
    Aug 15, 2018
    Posts:
    3
    Hello, i'm playing a 2D game made by Unity engine recently and i wonder how to make an effect appear on the mouse click location when you click. It's like this


    As you can see the circle effect appear when you right click any spot on game scene and the light dust when you hold and move the right mouse
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Click once, spawn a particle system with a burst. On click & hold, spawn a particle system for the light particles and stop it after you let go of the mouse. The second particle system is probably using some emission rate and emission over distance.

    Get the world coordinates of the mouse like this:
    Code (CSharp):
    1. Vector3 mouseWP = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    2.  
    3. //set mouseWP.z = desired depth
     
    khanpham1995 likes this.
  3. khanpham1995

    khanpham1995

    Joined:
    Aug 15, 2018
    Posts:
    3
    Thank you, " partical system" is what i am looking for
     
  4. gibsonvictor

    gibsonvictor

    Joined:
    Feb 22, 2021
    Posts:
    2
    how to achieve it efficiently

    like many clicks happens and spawning and destroying takes place
    can affect CPU performance
     
  5. gibsonvictor

    gibsonvictor

    Joined:
    Feb 22, 2021
    Posts:
    2
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class TouchDetection : MonoBehaviour
    {
    public GameObject particleEffect;
    public Canvas canvas;
    private GameObject effect;
    int counter = 0;
    // Update is called once per frame
    void Update()
    {
    if (Input.GetMouseButtonDown(0))
    {
    if (counter == 0)
    {
    effect = GameObject.Instantiate(particleEffect, Input.mousePosition, Quaternion.identity, canvas.transform);
    Invoke("DestroyEffect", 0.5f);
    counter++;
    }
    }
    }
    public void DestroyEffect()
    {
    Destroy(effect);
    counter = 0;
    }
    }
     
  6. DAsap

    DAsap

    Joined:
    May 8, 2015
    Posts:
    4
    implementing object pooling should be the best option to recycle the object