Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How Long is Mouse Button Held Down ?

Discussion in 'Scripting' started by Deleted User, Nov 13, 2015.

  1. Deleted User

    Deleted User

    Guest

    Anybody help with counting how long a mouse button is held down for and storing that value ?

    And if possible, how to get that value to increase by a set amount, i.e. 1, 10, 50, etc instead of numbers with decimals points.
     
    Last edited by a moderator: Nov 13, 2015
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    If (input.GetMouseButton(0)) {
    yourCounter += Time.deltaTime;
    }
     
    Deleted User likes this.
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    or you could just cast the float to an int before use.
     
    Deleted User likes this.
  4. Deleted User

    Deleted User

    Guest

    Thanks,

    I think I've got this basically working now, I can change the chargespeed to a set amount, i.e 1, 50, 100, etc so it increases that amount each time, is there a way to limit this so for instance, the chargelevel never exceeds a maximum of 1000 ?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MouseCount : MonoBehaviour {
    5.  
    6.     public float ChargeLevel = 0f;
    7.     public int ChargeSpeed = 100;
    8.  
    9.     void Update () {
    10.         if (Input.GetMouseButton(0))
    11.         {
    12.             ChargeLevel += Time.deltaTime * ChargeSpeed;
    13.         }
    14.         if (Input.GetMouseButtonUp(0))
    15.         {
    16.             ChargeLevel = 0f;
    17.         }
    18.     }
    19. }
     
    Last edited by a moderator: Nov 13, 2015
  5. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Chargelevel = (chargelevel > 999 ? 0 : chargelevel) ;
     
    Deleted User likes this.
  6. Deleted User

    Deleted User

    Guest

    Thanks, code dummy here btw, I don't understand the logic, if your code translates to plain english, how would that work in relation to my code ?

    so, I'm assuming your checking to see if the chargelevel is greater than 999, then what does the ? 0 : chargelevel part do ?
     
  7. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    It's a one-line if it translates to if chargelevel > 999 then return 0 else return chargelevel, and after this I set chargelevel to this return value
     
    Deleted User likes this.
  8. Deleted User

    Deleted User

    Guest

    Thanks for spelling that out and explaining it for me, I've implemented that and it works great, resetting the ChargeLevel back to 0 again !

    I can also replace the 0 value with 1000, then it doesn't reset, that way it will always hit a maximum value when the mouse is held down long enough. Thank you, you've been a fantastic help with this so far.

    I've modified my code : -

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MouseCount : MonoBehaviour {
    5.  
    6.     public float ChargeLevel = 0f;
    7.     public int ChargeSpeed = 100;
    8.     public int MaxCharge = 1000;
    9.    
    10.     void Update () {
    11.         if (Input.GetMouseButton(0))
    12.         {
    13.             ChargeLevel += Time.deltaTime * ChargeSpeed;
    14.             ChargeLevel = (ChargeLevel > 999 ? MaxCharge : ChargeLevel);
    15.         }
    16.         if (Input.GetMouseButtonUp(0))
    17.         {
    18.             ChargeLevel = 0f;
    19.         }
    20.     }
    21. }
    22.  
     
  9. rchmiel

    rchmiel

    Joined:
    Apr 5, 2015
    Posts:
    49
    Or put this to function:
    Code (csharp):
    1. public float Chargelevel()
    2. {
    3.    if(chargelevel > 999) return 0;
    4.    else return chargelevel;
    5. }
     
    Deleted User likes this.
  10. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Uhm, rchmiel, I did the same thing, but in one line. Functions are for things that you use often.
     
    Deleted User likes this.
  11. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    You could also make the code shorter, usinf this:
    Chargelevel = (chargelevel >= MaxCharge ? MaxCharge : chargelevel + Time.deltaTime * ChargeSpeed) ; instead of those two lines
     
    Deleted User likes this.
  12. rchmiel

    rchmiel

    Joined:
    Apr 5, 2015
    Posts:
    49
    Yes, it depends on what you want to use.
     
  13. Deleted User

    Deleted User

    Guest

    Thanks, all super helpful, the code works great !

    Overall, this script will be expanded upon to add force in a forward motion to my player character, see this thread, couldn't seem to get any help with that one though : -

    http://forum.unity3d.com/threads/pa...-back-and-add-force-at-desired-amount.367523/

    So, with your help, this is where I currently am, I also have a seperate script that handles which direction the player is facing ( mouselookat ) : -

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MouseCount : MonoBehaviour {
    5.  
    6.     public float ChargeLevel = 0f;
    7.     public int ChargeSpeed = 100;
    8.     public int MaxCharge = 1000;
    9.    
    10.     void Update () {
    11.         if (Input.GetMouseButton(0))
    12.         {
    13.             ChargeLevel += Time.deltaTime * ChargeSpeed;
    14.             ChargeLevel = (ChargeLevel > 999 ? MaxCharge : ChargeLevel);
    15.  
    16.             // Alternate way to calculate
    17.             // ChargeLevel = (ChargeLevel >= MaxCharge ? MaxCharge : ChargeLevel + Time.deltaTime * ChargeSpeed );
    18.         }
    19.         if (Input.GetMouseButtonUp(0))
    20.         {
    21.  
    22.             GetComponent<Rigidbody>().AddForce(-transform.forward * ChargeLevel);
    23.             GetComponent<Rigidbody>().useGravity = true;
    24.  
    25.             ChargeLevel = 0f;
    26.         }
    27.     }
    28. }
     
    Last edited by a moderator: Nov 13, 2015
    AusMicro likes this.