- Home /
Fixed it by myself.
Make footstep script
Hi! There is a scene in my game that have water., and i wanted to make a script that add (or play) footstep sounds when my character walks in the water. Can someone help me? I have been trying to do this about a week for now and i didn't get any result.
I wanted to make something like this:
The player is walking normaly in the map, making it's default footstep sounds (it is a First Person Character). Then he enters in the water. EVERY TIME he walk, a sound is played. There are two different sounds, one for each foot.
The problem is that i don't know how to make the sound repeat or something, and i don't know how to get two sounds to play at the same time =\ I am using JavaScript, but i would accept C# too.
Here's my script:
#pragma strict
function OnTriggerEnter (o:Collider) {
//Below is the code to play the Audio Source attached to the Game Object (the water)
GetComponent.<AudioSource>().Play();
}
// The OnTriggerExit function is kinda disabled because i don't need to add anything on it.
function OnTriggerExit (o:Collider) {
}
This is a very simple Java-Script that play a sound attached to an audio source when the player hit the object's collider. In my case, the object with this script will be the water. Can someone help me? I don't have any idea to how to make this...
First, you have to check if the player is in the water. You can do this via Raycasting, or the OnTrigger function. Your choice. To play two audioclips simultaneously, you need two AudioSource components.
Yeah, but how do i reference the two? Do i just type for example:
GetComponent.<AudioSource1>().Play();
GetComponent.<AudioSource2>().Play();
Is that correct? And what about the detection? The OnTriggerFunction detects only when the player touch the trigger for the first time, and it doesn't register if the player have touched it again until it isn't there.
Answer by ZBlaxland · Oct 17, 2016 at 05:00 AM
Hopefully this helps :D
Unfortunately, SpeedTutors' videos doesn't work in Unity 5, almost every script that he made are uncompatible.
Answer by Naphier · Oct 18, 2016 at 07:04 AM
First, your game object can have multiple audio sources, your script should have a public AudioSource variable naming each sound (waterWalk, groundWalk, jumpLanding, etc). Second, you should do this with animation clip events. You can set up a trigger on them to run a method at specific points in the animation clip (let's call this method OnStep()). That script must be on the same game object as the animator controller. This script will need to know what type of terrain the player is on, this would likely be done through OnCollisionEnter then check the tag of the other collision (ground, water, etc). Then depending on that the just call waterWalk.Play() in the OnStep method.
Follow this Question
Related Questions
'Play' is not a member of 'Object' 1 Answer
Get key down looped sound? 0 Answers
Play a sound when flicker? 1 Answer
Sound Effect won't play! 1 Answer