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

Please help with GetMouseButtonDown

Discussion in 'Scripting' started by JustinC, Dec 6, 2014.

  1. JustinC

    JustinC

    Joined:
    Dec 3, 2014
    Posts:
    2
    Hello All,

    I am having a problem and my question keeps getting rejected through Unity Answers. I am trying to get some code that on the first click of Lft Mouse button everything slows down, if you click Lft Mouse again with in 4 seconds you teleport to the position you clicked. I have the slow time working and the teleport working, just cannot get the two clicks to happen separately. The only way I can think of doing it is an if(GetMouseButtonDown) function with another if(GetMouseButtonDown) inside of it.This is what I have, please any help would be greatly appreciated i have been stuck on this for going on three days now.

    Code (CSharp):
    1.    
    2.  
    3.     IEnumerator JumpWait()
    4.     {
    5.         yield return new WaitForSeconds(.5f);
    6.         Player.transform.position = new Vector3(tx, ty, tz);
    7.         yield return new WaitForSeconds (.25f);
    8.         particle.particleEmitter.enabled = false;
    9.     }
    10.  
    11.     IEnumerator NoJumpWait()
    12.     {
    13.         yield return new WaitForSeconds (2f);
    14.         Time.timeScale = 1f;
    15.     }
    16.  
    17.  
    18.     void Update()
    19.     {
    20.         if(Input.GetMouseButtonDown(0))
    21.         {
    22.             Time.timeScale = .5f;
    23.             Debug.Log("Mouse Click 1");
    24.  
    25.             if(Input.GetMouseButtonDown(0))
    26.             {
    27.                 Debug.Log ("Mouse Click 2");
    28.                 animator.SetBool("Jump", true);
    29.            
    30.                 RaycastHit hitInfo;
    31.                 Ray rayOrigin = Camera.main.ScreenPointToRay (Input.mousePosition);
    32.  
    33.                 if (Physics.Raycast (rayOrigin, out hitInfo, maxDist))
    34.                 {
    35.                     tx = hitInfo.point.x;
    36.                     ty = hitInfo.point.y;
    37.                     tz = hitInfo.point.z;
    38.                     particle.particleEmitter.enabled = true;
    39.                     StartCoroutine(JumpWait());
    40.                 }
    41.                 StartCoroutine (NoJumpWait());
    42.             }
    43.         }
    44.     }
    45.  
    46.     }
     
  2. PolymorphiK Games

    PolymorphiK Games

    Joined:
    Oct 5, 2014
    Posts:
    51
    Create a boolean that reset after 4 seconds. So you want to do is this:

    Event: Left Click

    Pseudo Code:
    // Variables needed
    bool isTimeSlowed = false;
    float elapsedTime = 0.0f;
    float doubleClickTime = 4.0f;

    // Logical Checks
    1.) if(isTimeSlowed == false) then slow down time and set isTimeSlowed = true
    2.) if(isTimeSlowed == true) then check if(elapsedTime < doubleClickTime) jump
    3.) if(elapsedTime < doubleClickTime) increment elapsedTime += Time.deltaTime
    4.) if(elapsedTime > doubleClickTime) set elapsedTime = 0.0f isTimeSlowed = false;

    Does this make sense?
     
  3. JustinC

    JustinC

    Joined:
    Dec 3, 2014
    Posts:
    2
    that makes sense i will get back in a little while, im gonna try it out
     
  4. PolymorphiK Games

    PolymorphiK Games

    Joined:
    Oct 5, 2014
    Posts:
    51
    If you get stuck let me know and I can code something up for you really quick :)
     
  5. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    Nice work. There's no need for a coroutine for something this simple, haha.