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. Dismiss Notice

How GetMouseButton dependes of time pressed?

Discussion in 'Scripting' started by ratg97, Aug 24, 2014.

  1. ratg97

    ratg97

    Joined:
    Aug 4, 2014
    Posts:
    65
    Hi i want do different actions depends of time pressed. i use csharp

    GetMouseButton
     
  2. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Use Time.time or Time.deltaTime
    Code (CSharp):
    1. if(Time.time < x) A();
    2. else B();
    just a very basic example.
     
    Last edited: Aug 24, 2014
  3. ratg97

    ratg97

    Joined:
    Aug 4, 2014
    Posts:
    65
    ok i do it... if a have problems i say you
     
  4. ratg97

    ratg97

    Joined:
    Aug 4, 2014
    Posts:
    65
    the variable x? is input.GetMouseButtonDown or what?
     
  5. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    No, it was just an example to what u could do.
    Code (CSharp):
    1. float time;
    2.  
    3.     void Update()
    4.     {
    5.         if (Input.GetMouseButtonDown(0))
    6.         {
    7.             time += Time.deltaTime;
    8.             if ((int)time > 10) time -= 10;
    9.  
    10.             switch ((int)time)
    11.             {
    12.                 case 0:
    13.                     A();
    14.                     break;
    15.                 case 1:
    16.                     B();
    17.                     break;
    18.                 case 2:
    19.                     C();
    20.                     break;
    21.                 case 3:
    22.                     //etc etc etc
    23.                     break;
    24.                 case 4:
    25.                     break;
    26.                 case 5:
    27.                     break;
    28.                 case 6:
    29.                     break;
    30.                 case 7:
    31.                     break;
    32.                 case 8:
    33.                     break;
    34.                 case 9:
    35.                     break;
    36.                 case 10:
    37.                     break;
    38.                 default:
    39.                     break;
    40.             }
    41.         }
    42.     }
    the % operator is also an option.
     
  6. ratg97

    ratg97

    Joined:
    Aug 4, 2014
    Posts:
    65
    mmmm this is my code how can i implement it ?

    Code (CSharp):
    1. using System.Collections;
    2.  
    3. public class PlayerMovement : MonoBehaviour {
    4.  
    5.     public float fuerzaSalto = 15f;
    6.     private bool corriendo = false;
    7.     public float velocidad = 1f;
    8.  
    9.     public float distanceTraveled;
    10.  
    11.     private bool enSuelo = true;
    12.     public Transform comprobadorSuelo;
    13.     private float comprobadorRadio = 0.07f;
    14.     public LayerMask mascaraSuelo;
    15.  
    16.     private void GameStart () {
    17.        
    18.         distanceTraveled = 0f;
    19.         GUIManager.SetDistance (distanceTraveled);
    20.     }
    21.  
    22.  
    23.     void Start () {
    24.  
    25.         NotificationCenter.DefaultCenter().AddObserver(this, "PersonajeHaMuerto");
    26.    
    27.     }
    28.  
    29.     void FixedUpdate(){
    30.  
    31.     if(corriendo){
    32.  
    33.         rigidbody2D.velocity = new Vector2(velocidad, rigidbody2D.velocity.y);
    34.  
    35.     }
    36.  
    37.         enSuelo = Physics2D.OverlapCircle(comprobadorSuelo.position, comprobadorRadio, mascaraSuelo);
    38.  
    39.     }
    40.    
    41.  
    42.     void Update () {
    43.  
    44.         distanceTraveled = transform.localPosition.x;
    45.         GUIManager.SetDistance(distanceTraveled);
    46.         distanceTraveled = Mathf.Round (distanceTraveled);
    47.  
    48.         if(Input.GetMouseButtonDown(0)){
    49.             if(corriendo){
    50.                 if(enSuelo){
    51.                     rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, fuerzaSalto);
    52.                     Debug.Log("1");
    53.  
    54.                     }
    55.  
    56.                 }
    57.             }else{
    58.                 corriendo = true;
    59.                 NotificationCenter.DefaultCenter().PostNotification(this, "PersonajeEmpiezaACorrer");
    60.             }
    61.         }
    62. }
     
  7. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    You didn't code this did you? Õ 3 Õ
     
  8. ratg97

    ratg97

    Joined:
    Aug 4, 2014
    Posts:
    65
    yes i am spanish i only need with your code implement here:

    Code (CSharp):
    1.         if(Input.GetMouseButtonDown(0)){
    2.             if(corriendo){
    3.                 if(enSuelo){
    4.                     rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, fuerzaSalto);
    5.                     Debug.Log("1");
    6.                     }
    7.                 }
    8.             }else{
    9.                 corriendo = true;
    10.                 NotificationCenter.DefaultCenter().PostNotification(this, "PersonajeEmpiezaACorrer");
    11.             }
    have you got skype?
     
  9. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    I don't know what all of your booleans do, so you should do it instead.
    Just copy & paste it at the spot where you want to call a random method. .-.
     
  10. ratg97

    ratg97

    Joined:
    Aug 4, 2014
    Posts:
    65
    cand you add me on skype: santimurciano
     
  11. ratg97

    ratg97

    Joined:
    Aug 4, 2014
    Posts:
    65
    I do this but the player not jump

    Code (CSharp):
    1. if(Input.GetMouseButtonDown(0)){
    2.             if(corriendo){
    3.                 if(enSuelo){
    4.                     time += Time.deltaTime;
    5.                     if ((int)time > 10){
    6.                         time -= 10;      
    7.                     }
    8.                     switch ((int)time){
    9.                     case 0:
    10.                         if ((int)time > 1){
    11.                         rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, fuerzaSalto);
    12.                         Debug.Log("1");
    13.                         }
    14.                         break;
    15.                     case 1:
    16.                         if ((int)time > 2){
    17.                         rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, fuerzaSalto+3);
    18.                         Debug.Log("2");
    19.                         }
    20.                         break;
    21.                     case 2:
    22.                         if ((int)time > 3){
    23.                         rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, fuerzaSalto+5);
    24.                         Debug.Log("3");
    25.                             }
    26.                         break;
     
  12. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    It's from 0 to 10, change if statement if u want it to only go to 3.
    if you just want to have a random value, why not use Random.Range or Random.value?
    whatever o-o

    Code (CSharp):
    1. if ((int)time > 2){
    2. time -= 2;    
     
  13. ratg97

    ratg97

    Joined:
    Aug 4, 2014
    Posts:
    65
    i think that is not my idea that i want... is there other form?