C# - OnMouseDown vs OnMouseButtonDown - How can I rewrite this, make it better?

Hey!

I have a Well and as a child a particle system of water splashing.
If I click the well, it plays an animation (gets bigger then smaller) and then plays my particle system which is the water splashing.

However, If i hold down the mouse the particlesystem continuously plays, and if i do OnMouseButtonDown it doesnt play at all.
The thing is, you will be clicking the well very often, (spam clicking for water) now is it better to instantiate the particleFX for x Time then delete it after y Time? or what should I do, and preferably how?

public class ClickAnimation : MonoBehaviour
{
    public GameObject MyAnimatorObject;
    private Animator myAnimator;

    public bool waterPlay;
    public ParticleSystem waterFountain;


    void Start ()
    {
        myAnimator = MyAnimatorObject.GetComponent<Animator> ();
    }
       
    void Update ()
    {
        if (waterPlay)
        {
            waterFountain.Play ();
            waterFountain.GetComponent<ParticleSystem>().enableEmission = true;
        }

        else if (!waterPlay)
        {
            waterFountain.Stop();
        }
    }
       

    void OnMouseDown()
    {
        myAnimator.Play ("WellClicked");
        waterPlay = true;
    }

    void OnMouseUp()
    {
        waterPlay = false;
    }

}

It’s hard to say how to do it without properly defining what you want to happen.

I would suggest ditching OnMouseXXX in favour of the EventSystem interfaces.

Pretty much in easier to picture terms:

A clicker game with a chest, each time u click on it, the chest shakes (plays the animation) and has few coins from the Particle system fall out, lets say 3-4 coins (predetermined in the settings of the particle system)

In my case its just a fountain and water splashing out :slight_smile:
But each time it gets a click, I guess it would be nicer to finish the particlesystem going on (e.g. the 3-4 coins falling out) and let an extra 3-4 fall out.

(PS: this is not like a normal clicker, so you dont click the “coins” or water to pick up later. hence the reason why i am using a particle system and not gameObjects.)

I would use one particle system. I would turn off envision. Then I would call ParticleSystem.Emit on each click. I think that will suit your needs better.

Thanks, didnt know about that, but running into an error since I am not sure how to implement it
I added this which is causing the error:

    public void Emit (int count);

My Error: “must have a body because it is not marked abstract, extern, or partial.”
and changed the Update and made it into:

void Update () {
        if (waterPlay) {
            waterFountain.Play ();
            waterFountain.GetComponent<ParticleSystem>().Emit (5);
        }
        else if (!waterPlay){
            waterFountain.Stop();
        }
    }

Delete that line altogether.

Yay, got it! :slight_smile:
Also had to get rid off the waterPlay bool and move the Emit(5) down to OnMouseDown and everything works :slight_smile:

Thanks a lot

1 Like