- Home /
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!
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.
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)
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;
}
}
Updated! O$$anonymous$$ go check it out now ;) - Btw you are using footstep sounds right as moveSound
?
Your answer
Follow this Question
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