How do you make a charged attack when i'm listening from an OnAttack event?

Hello Everyone,

Me and my friend are making a top down RPG.
My Player excists of multiple scipts like Player Input , Player Movement, Player Attack script, …
But the problem is in my player Input i fire an event when i press mouse 1 and in my player attack i listen to that event.
now i can do normal attacks but how can i add charged attacks this way?
Im trying to obey the SOLID principles so i dont want my scripts to be closely coupled.
i’m quite new to it so maybe i’m doing it wrong.

Any help will be appreciated!

You must first define what a “charged attack” is?

If it’s something where you have some meter that has to fill up, then check the meter and react accordingly.

If you mean something where they must hold the button for a duration… well you’ll have to check for down/up of the mouse and react accordingly.

If you mean something else… well what do you mean? Programming languages do not know the concept of run/jump/attack/etc… your entire job as a programmer is to define/describe these acts in the language available to you.

basically…

Not enough information in your post to give you any real direction relative to your work.

So for example what i want to implement is when i press left mouse button then i shoot a normal fireball but when i hold it i charge the spell so it becomes a bigger fireball.
this all depends on the spells the player have some spells have a charged attack and some dont.

How do i know if the user just pressed it or holds the button for a longer duration.
And can i let this work with events ?

I hope this is a better explanation.

Well this sounds like a mouse up situation (since you don’t want to react until they’ve released the button).

As for getting this in an event form (without getting into the effectiveness of using a polling vs event system). You’re going to have to translate the polling aspect of unity input and adapt it to your event model.

NOTE - I’m going to use OG Unity Input class, and C# events, for this example:

public class PlayerInput : MonoBehaviour
{

    public event System.Action<float> PressedAttack;

    private float _mouseDownTime;

    private void Update()
    {
        if(Input.GetMouseButtonDown(0)) //using mouse button 0 here
        {
            _mouseDownTime = Time.unscaledTime;
        }
        else if(Input.GetMouseButtonUp(0))
        {
            float dur = Mathf.Max(Time.unscaledTime - _mouseDownTime, 0f); //clamp it above 0 in case of oddities
            this.PressedAttack?.Invoke(dur);
        }
    }

}

Here I just include the amount of time that passed while holding the mouse button (in this example it’s left mouse button).

Then in your PlayerAttack you check that duration. If it’s short, they did a regular attack. If it’s long, they did a charged attack.

What constituted short/long is up to you.

Note I personally would refactor this into a generalized form that can do any input.

This is exactly what is was looking for!
Thank you!