Increase value with button time hold?

I’m trying to have a base value multiplied by the value of time i hold the left mouse. Then, once I release the mouse button I want it to initiate an action with that value. I would also like to have a maximum hold time where once It reaches that value it initiates the action. So how would I do that? Thanks in Advance :smiley:

The solution should be similar to this http://answers.unity3d.com/comments/1263686/view.html

@Brylos you could probably use this. It´s a modifications of the other answer @wizards_and_mages gave.

using UnityEngine;

public class YourClass : MonoBehaviour
{
    public float startTime;
    public float powerUpSpeed;
    public float currentPower;
    public float maxPower;
    public bool increasingPower;

    private float growRate = 0;

    private void Update()
    {
        if (Input.GetMouseButtonDown(0) && !increasingPower)
        {
            startTime = Time.time;
            growRate = (maxPower - currentPower) / powerUpSpeed;
            increasingPower = true;
        }
        else if (startTime + powerUpSpeed > Time.time && increasingPower)
        {
            currentPower = Mathf.MoveTowards(currentPower, maxPower, growRate * Time.deltaTime);
        }
        else if(startTime + powerUpSpeed < Time.time && increasingPower)
        {
            currentPower = maxPower;
            startTime = 0;
            growRate = 0;

            increasingPower = false;
        }
    }
}

This should do it

class YourClass : MonoBehaviour
{
        private float startTime;
        private float baseLinePower;
        private float currentPower;
        private float maxPower;

        private void Update()
        {
                if(Input.GetKeyDown(KeyCode.YourKey)
                {
                         startingTime = Time.time;
                }
                if(Input.GetKey(KeyCode.YourKey)
                {
                        if(currentPower < maxPower)
                        {
                                currentPower += (Time.time - startingTime) * baseLinePower;
                        }
                }
        }
}

Keep in mind I wrote that here not in Unity so it’s not tested but it should work.

Hi There @Brylos, made an example for you. Here’s the unity package so you can just open that in your project and see it for yourself. If this answer helped, please accept it, it’d be much appreciated!

Here’s the code with explanatory comments!

using UnityEngine;
using System.Collections;

public class ChargedShotExample : MonoBehaviour {

    // Your target. (taken from your code)
    public GameObject Ball;

    public float m_powerMin = 1f;               // Lowest amount of power to fire with.
    public float m_powerMax = 5f;              // Highest amount of power to fire with.
    public float m_chargeDurationMax = 2f;      // Time at which auto-fire happens.
    private float m_chargeDurationCurrent = 0f; // Time spent holding left mouse button down.
    private bool m_isCharging = false;          // Currently charging?
    private bool m_mouseWasReleased = true;

	void Update () {

	    if( Input.GetMouseButtonDown( 0 ) && m_mouseWasReleased )
        {
            // Started Charging
            m_chargeDurationCurrent = 0f;
            m_isCharging = true;
            m_mouseWasReleased = false;
        }
        else if ( m_isCharging && (Input.GetMouseButtonUp(0) || m_chargeDurationCurrent >= m_chargeDurationMax))
        {
            // Release or Auto-release.
            float power = Mathf.Lerp( m_powerMin, m_powerMax, m_chargeDurationCurrent / m_chargeDurationMax );

            // Your method.
            Fire( power );
            Reset();
        }

        // Currently Charging.
        if (m_isCharging)
        {
            m_chargeDurationCurrent += Time.deltaTime;
        }

        // Only after you release the mouse button, we will allow the next shot.
        // If the shot was called via hitting the charge duration max, this will prevent from infinitely firing every X duration.
        if( !m_mouseWasReleased )
        {
            m_mouseWasReleased = Input.GetMouseButtonUp( 0 );
        }
    }

    private void Fire(float power)
    {
        // Apply Force to your ball ;-)
        Debug.Log( "Test: " + power );
        if ( Ball != null )
        {
            Ball.GetComponent<Rigidbody>().AddForce( Ball.transform.forward * power, ForceMode.Impulse );
        }
        else
        {
            Debug.Log( "Ball reference was null, please go to the inspector of the object: " + this.name + " and drag in any target gameObject with a rigidbody to fix this." );
        }
    }

    private void Reset()
    {
        // Reset Current Duration.
        m_chargeDurationCurrent = 0f;
        m_isCharging = false;
    }
}