Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by JohnWatson · Sep 05, 2013 at 01:32 PM · javascriptvariablesoundgrounded

Walking Sound Script not working properly (JavaScript)

I'm fairly new to Unity so I have some problems. So I need Help with some things. I have been hacking around this Grounded variable for a while and finally got it working but now the audio only stops and doesn't start playing any more when I touch the ground. also all my Keypresses override my old keypress (which is proably what stops the audio from restarting when touching the ground) for example:

  1. I'm holding down the "w" key: audio starts playing (as it should)

  2. while holding down the "w" key I start holding down the "d" key: *audio restarts from beginning

  3. I then let go of the "d" while still holding the "w" key: audio stops

    pragma strict

    var isGrounded : boolean = true; var controller : CharacterController = GetComponent(CharacterController); var Sound : AudioClip;

    function Start () {

    } function Update () { if(!controller.isGrounded){ { audio.clip = Sound; audio.Stop(); }

      } else {
         
         if((Input.GetKeyDown("w")||Input.GetKeyDown("a")||Input.GetKeyDown("s")||Input.GetKeyDown("d")) && isGrounded)
         {
           audio.Play();
         }
      
         if((Input.GetKeyUp("w")||Input.GetKeyUp("a")||Input.GetKeyUp("s")||Input.GetKeyUp("d")) && isGrounded)
         {
            audio.Stop();
         }
     }
     }
    
    
    

I hope somebody can help. Thanks in advance!

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ShadoX · Sep 12, 2013 at 07:44 AM 0
Share

You could try something like...

 var walking : boolean = false;
 if(Input.Get$$anonymous$$eyDown("w")||Input.Get$$anonymous$$eyDown("a")||Input.Get$$anonymous$$eyDown("s")||Input.Get$$anonymous$$eyDown("d"))
     {
          walking = true;
     }
  
     if(Input.Get$$anonymous$$eyUp("w")||Input.Get$$anonymous$$eyUp("a")||Input.Get$$anonymous$$eyUp("s")||Input.Get$$anonymous$$eyUp("d"))
     {
        walking = false;
     }
 
 if(walking){ // checks if walking == true just a shorter form of it
       audio.clip = Sound;
       audio.Play();
 
 } else {
    audio.Stop();
 }

[edit] Sorry, just realized that you would still get the same problem with this code so please ignore this and just use the Answer from Vexe

avatar image JohnWatson · Sep 12, 2013 at 08:25 AM 0
Share

Thanks for trying anyways :D

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by vexe · Sep 12, 2013 at 07:58 AM

First and foremost you need an extra check, make sure that the sound is not 'already' playing before you play it:

if (... && !audio.isPlaying) audio.Play();

But that won't totally fix it, so let's check out what you said and try to go from there:

1- "while holding down the "w" key I start holding down the "d" key: *audio restarts from beginning" - That's right, that's what you told it to here:

 if((Input.GetKeyDown("w") || Input.GetKeyDown("a") || Input.GetKeyDown("s") ||   
     Input.GetKeyDown("d")) && isGrounded) {
     audio.Play();
 }

Which basically translates to: "any time I press 'w', 'a', 's' or 'd' and I'm grounded" -> play the sound from the beginning.

2- "I the let go of the "d" while still holding the "w" key: audio stops" That's right again, that's what you told it to do here:

 if((Input.GetKeyUp("w") || Input.GetKeyUp("a") || Input.GetKeyUp("s") ||
    Input.GetKeyUp("d")) && isGrounded) {
    audio.Stop();
 }

Which basically says: "If I let go off of 'w', or 'a', or 's', or 'd' and I'm grounded" -> stop the sound.

So let's get your comparison operators right, and knock out your confusion about Input.GetKey/Input.GetButton family.

  1. Input.GetButtonDown/Input.GetKeyDown returns true on the first frame you press button on, and then as frames progress, holding down the button, Input.GetButtonDown/Input.GetKeyDown will return false.

  2. To say "if I'm holding X button" use Input.GetButton/Input.GetKey instead, which will return true while the button is being pressed.

  3. Input.GetButtonUp/Input.GetKeyUp returns true the moment you let go of the key (key goes up)

More info on Input.GetKey and Input.GetButton families can be found here.

Armed with the information in your possession now, you should be able to fix the problem and know what went wrong with your approach :)

In case you're lazy here's how it could look like:

 function Update()
 {
     var isMoving = Input.GetKey("w") || Input.GetKey("a") || Input.GetKey("s") || Input.GetKey("d");

     // which translates to: "If I'm holding 'w', 'a', 's' or 'd' -> I'm moving"

     if(isMoving && controller.isGrounded)
     {
        if (!audio.isPlaying)
        {
           audio.Play();
        }
     }
     else audio.Stop();
 }

Comment
Add comment · Show 19 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image vexe · Sep 12, 2013 at 08:28 AM 0
Share

Let me know how it goes :) - Did that solve the problem? - If so, please mark the answer as correct to close this question so that others don't think it's still active and we could move on :)

avatar image JohnWatson · Sep 12, 2013 at 08:28 AM 0
Share

So basicly I make a new variable preventing Audio playing whe it already is, so that's good to know and the I delete all the "Down"s and leave the "Up"s am I getting this right ? I'm going to try it now. Thank you very much.

edit: I'm confused about you having is$$anonymous$$oving and isWalking how does the script know that is$$anonymous$$oving corresponds to isWalking? or is that just a mistake?

avatar image vexe · Sep 12, 2013 at 08:32 AM 0
Share

No you don't need to create a boolean to deter$$anonymous$$e if the audio is playing or not, it's already a member declared inside the AudioSource class, named isPlaying

And forget about the "Up"s - just try the code I gave you.

avatar image JohnWatson · Sep 12, 2013 at 08:36 AM 0
Share

I'm sorry if I sound stupid asking this stuff, but I don't only want to get it to work i want to know hw it works since I'm looking forward to keep working with Unity and scriping in general. Thanks!

avatar image vexe · Sep 12, 2013 at 08:38 AM 0
Share

No need to think you sound stupid :) - We all exist to learn, best learning comes from simple questions and deep understanding :) - But what is it that you're not quite getting? - what is it that you want to know how it works?

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Gun Script Help 2 Answers

get variables from 1 JavaScript and influence them in different script 0 Answers

How to find the opposite variables list? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges