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

Input KeyDown that turns off when you take your finger off the key?

Discussion in 'Scripting' started by mcgamerx19, Aug 22, 2014.

  1. mcgamerx19

    mcgamerx19

    Joined:
    Jul 23, 2014
    Posts:
    19
    I'm new to javascript and working on a piece that will have walking sounds in the world when you press W. But how can I create it so when you press W the sound will play and if you take it off it will stop playing.

    Because right now it just creates a sound and plays the whole walking sound. So I can tap W and it will create a bunch of walking sound bits in the game. Here's my code
    Code (CSharp):
    1. function Update () {
    2.    
    3.     if (Input.GetKeyDown(KeyCode.W))
    4.     {
    5.         AudioSource.PlayClipAtPoint(walkingoutside, transform.position);
    6.     }
    7. }
     
  2. dakka

    dakka

    Joined:
    Jun 25, 2010
    Posts:
    113
    Input.GetKeyUp(KeyCode.W)
     
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    Input.GetKey returns true when the button is held down, and false when it is not. So that's half the problem; it can give you the current state of the key.

    The other half is: once you tell an AudioSource to play, it will play the whole sound, unless you tell it to stop. Fortunately there's a Stop method for exactly that purpose. So something like the following ought to work:

    Code (csharp):
    1.   if (Input.KeyDown(KeyCode.W)) {
    2.     audioSource.PlayClipAtPoint(walkingoutside, transform.position);
    3.   } else {
    4.     audioSource.Stop();
    5.   }
    Incidentally, if you're new to JavaScript, then let me recommend you be new to C# instead. The JavaScript support in Unity is great for people who are already fluent in JS and don't have the time or inclination to switch. But serious Unity coding is almost always done in C# (the PlayStation team here at the Unity conference said it was well over 95%, maybe 99% of the code they see on that platform). If you're new to both, I don't think one is particularly harder than the other, and in the long run I think you'll be happier with C#. That's my $0.02, anyway.

    Cheers,
    - Joe
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    Joe, your code will play the sound for 1 frame then immediately stop it.
    Code (csharp):
    1.  
    2. if (Input.GetKeyDown(KeyCode.W)) {
    3.     AudioSource.PlayClipAtPoint(walkingoutside, transform.position);
    4. }
    5. if (Input.GetKeyUp(KeyCode.W)) {
    6.     AudioSource.Stop();
    7. }
     
    Last edited: Aug 22, 2014
  5. dakka

    dakka

    Joined:
    Jun 25, 2010
    Posts:
    113
    I think he meant GetKey()
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    This would still be the wrong behavior, as it would generate a new object playing sound every frame the key is held.
     
  7. dakka

    dakka

    Joined:
    Jun 25, 2010
    Posts:
    113
    Ah good point, was looking at the input rather than the audio.
     
  8. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    Oops, that was a typo, I did mean GetKey. And StarManta, you're right, it would restart the sound every frame. Sheesh, that's what I get for posting before breakfast. Apologies for the confusion! StarManta's version looks correct to me.
     
  9. mcgamerx19

    mcgamerx19

    Joined:
    Jul 23, 2014
    Posts:
    19
    Unknown identifier: 'audioSource'.
     
  10. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    Should be capitalized I guess.
     
  11. mcgamerx19

    mcgamerx19

    Joined:
    Jul 23, 2014
    Posts:
    19
    Ok. Capitalized. Now I have 2 errors from that.

    "Unknown identifier 'audioSource' (From the "AudioSource.Stop())"

    &

    "An instance of type 'UnityEngine.AudioSource' is required to access non static member 'Stop'." from "AudioSource.Stop" also..
     
  12. mcgamerx19

    mcgamerx19

    Joined:
    Jul 23, 2014
    Posts:
    19
    Ok I messed around with it a little and I can't get past the "AudioSource.Stop()" error. It says " An instance of type 'UnityEngine.AudioSource' is required to access non static member 'Stop'."
     
  13. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    ....oh, right. OK, you're going to have to implement a bit of your own version of PlayClipAtPoint, one that returns the generated AudioSource. Basically PlayClipAtPoint creates a temporary GameObject with an AudioSource, then throws it to the wind to do its thing.

    Something like this:
    Code (csharp):
    1.  
    2. public static AudioSource PlayClipAtPointImproved(AudioClip clip, Vector3 position) {
    3. GameObject newGO = new GameObject("AudioSource for "+clip.name);
    4. AudioSource rtn = newGO.AddComponent<AudioSource>();
    5. rtn.clip = clip;
    6. rtn.Play();
    7. return rtn;
    8. }
    9.  
    10. private AudioSource storedSource;
    11. void Update() {
    12. if (Input.GetKeyDown(KeyCode.W)) {
    13.     storedSource = PlayClipAtPointImproved(walkingoutside, transform.position);
    14. }
    15. if (Input.GetKeyUp(KeyCode.W) && storedSource) {
    16.     storedSource.Stop();
    17. Destroy(storedSource.gameObject);
    18. }
    19. }
    20.