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!

Who’s your hero today? - Me :slight_smile: - 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.