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
1
Question by JohnWatson · Sep 12, 2013 at 01:12 PM · javascriptcharacterjumpsound

Impact Sound When Jumping? (JavaScript)

Hey, after alot of help from vexe I have managed to make a Footstep Sound Script now I'm trying to make a second sound to play whenever I (default character controller for now) hit the Ground so basicly after Jumping. Here's what I've got so far (Update):

     #pragma strict
 
 
 var moveSound : AudioClip;
 var landSound : AudioClip;
  
 private var hasJustLanded : boolean;
  
 // var isGrounded : boolean = true; what you need this for? - the controller already has that variable defined for it (controller.isGrounded)
 var controller : CharacterController = GetComponent(CharacterController);     
  
 function Start () {
     // by default, let your audio source plays the moving sound
     audio.clip = moveSound;
 }
  
 function Update()
 {
    var isMoving = Input.GetKey("w") || Input.GetKey("a") || Input.GetKey("s") || Input.GetKey("d");
  
    if(controller.isGrounded)
    {
         if (isMoving)
         {
              if (!audio.isPlaying)
              {
                  audio.Play();
              }
              if (!hasJustLanded) 
              {
                  hasJustLanded = true;
                  Debug.Log("yo I just landed, where you at?");
                  // play our new land sound...
                  audio.PlayOneShot(landSound);
              } 
         } else if (audio.isPlaying) audio.Stop();
      }
      else 
      {
          if (audio.isPlaying) audio.Stop();
          Debug.Log("yo check dis out am flyin! :D");
          hasJustLanded = false;
      }
   }

I apreciate any help. Thanks in advance!

Comment
Add comment
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

1 Reply

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

Answer by vexe · Sep 12, 2013 at 01:23 PM

Who's your hero today? - Me :) - You need to detect the moment you hit the ground with. This little snippet does just that - Just stick this in with the rest of the stuff you got and you should be good to go.

 private var hasJustLanded = false;
 function Update()
 {
      if (controller.isGrounded) {
          if (!hasJustLanded) 
          {
               hasJustLanded = true;
                 Debug.Log("yo I just landed, where you at?");
               // play your extra sound here...
          } 
      } 
      else
      {
         if (audio.isPlaying) audio.Stop();
         Debug.Log("yo check dis out am flyin! :D");
          hasJustLanded = false;
      }
 }

EDIT: Please work on improving your programming skills, I'm asking you this for your own good, you won't go too far without good programming skills, watch/read more tutorials. This should be a simple edit/insert to you previous code.

OK, first things first - I'm gonna assume that you only need 2 clips, one for moving (running sounds for example) and other for falling/landing on the ground. You should make those clips public and assign them via the inspector.

Please excuse my JS syntax, I'm by no mean a JS coder.

pragma strict

 // NEW STUFF
 // Inspector variables
 var moveSound : AudioClip;
 var landSound : AudioClip;
 
 private var hasJustLanded = true; // if you set this to false, the clip will play when you start the game - see the logic below of how this will happen.

 // var isGrounded : boolean = true; what you need this for? - the controller already has that variable defined for it (controller.isGrounded)
 var controller : CharacterController = GetComponent(CharacterController);     
  
 function Start () {
     // by default, let your audio source plays the moving sound
     audio.clip = moveSound;
 }

 function Update()
 {
     var isMoving = Input.GetKey("w") || Input.GetKey("a") || Input.GetKey("s") || Input.GetKey("d");
     if (controller.isGrounded) {
         if (isMoving) {
             if (!audio.isPlaying) {
                 audio.Play();
             }
         }
         else if (audio.isPlaying) audio.Stop();
         if (!hasJustLanded) {
             hasJustLanded = true;
             Debug.Log("yo I just landed, where you at?");
             // play your extra sound here...
             AudioSource.PlayClipAtPoint(landSound, transform.position);
             Debug.Log("BOOM");
         }
     }
     else {
         if (audio.isPlaying) audio.Stop();
         Debug.Log("yo check dis out am flyin! :D");
         hasJustLanded = false;
     }
 }

Wondering about PlayClipAtPoint? It creates an audio source at the position you specify and plays the clip you give it at that position. More info here.

I know the code looks a bit freaky now, but try it out and tell me if it works or not, in the meantime I will try to simplify it for you.

Comment
Add comment · Show 45 · 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 02:19 PM 1
Share

Updated - Let me know how it goes.

avatar image vexe · Sep 12, 2013 at 02:48 PM 1
Share

The thing is I'm also testing on a controller, but the way I'm moving is different from you, other than that I'm using exactly what I wrote for you. BTW if you want a good character controller setup, check out Quilly's tut (let this be after we settle this out)

avatar image vexe · Sep 12, 2013 at 03:04 PM 1
Share

O$$anonymous$$, I'm gonna split them, try this:

 function Update()
 {

 // OLD

  var is$$anonymous$$oving = Input.Get$$anonymous$$ey("w") || Input.Get$$anonymous$$ey("a") || Input.Get$$anonymous$$ey("s") || Input.Get$$anonymous$$ey("d");
  
     if(is$$anonymous$$oving && controller.isGrounded)
     {
        if (!audio.isPlaying)
        {
            audio.Play();
        }
     }
     else audio.Stop();


 // NEW

     if (controller.isGrounded) 
     {
        if (!hasJustLanded)
        {
          hasJustLanded = true;
          Debug.Log("yo I just landed, where you at?");
          // play your extra sound here...
          audio.PlayOneShot(landSound);
        }
     }
     else
     {
       Debug.Log("yo check dis out am flyin! :D");
       hasJustLanded = false;
     }
 }
avatar image vexe · Sep 12, 2013 at 03:58 PM 1
Share

Updated! O$$anonymous$$ go check it out now ;) - Btw you are using footstep sounds right as moveSound?

avatar image vexe · Sep 12, 2013 at 04:06 PM 1
Share

I'm giving you premium support lol

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

15 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

Related Questions

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

Setting Scroll View Width GUILayout 1 Answer

Translation of an object 2 Answers

How to stop song when i dont hold W A S or D 1 Answer

Particular "Jump" Script. 2 Answers


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