WHOOSH sound when passing nearby objects

Hey all.

How can I play a whooshing sound when passing nearby objects ? I’m not good with coding at all, so the only way I can think of is making a huge sphere on the player that will act as a hitbox that will play an audio when it collides with objects and will reset after like a few milliseconds. Not an accurate solution, but close enough.

Any other ideas on how to create such an effect ? If not, can you help me with how to make the sphere that will play an audio on collision ?

Is that like racing game or ?

In the sphere collider, enable trigger:
[×] Trigger

then,

It’s like a 3D city jumper where you pass through buildings and jump through the rooftops. So I’ll just parent a sphere collider to the player with this JS. But how can I induce the audio ?

Anyone ?

You can assign your audio clip by dragging it and dropping it in the inspector of your player object (or whatever your script is attached to) and it’ll create the component it needs to use it. In your script you can just use audio.Play();

To get a quick understanding of what you can do, check out the docs here:

The entries for clip, isPlaying, Play, Pause, Stop and PlayOneShot are likely to be of most interest to you right now.

It works kinda buggy. Sometimes it plays right, sometimes it delays, sometimes it doesn’t play at all. But I think it can work if I specify which mesh the box should collide to play the audio. Can I do that ?

You could raycast outward from the sides of the players head (ears) and check the current velocity of the player when you get a hit on the building layer. You can update a bool with that code and elsewhere play seamlessly looping audio as long as that bool is true.

This will give you a good amount of control for passing by objects that have variable sizes, and only playing the sound when moving fast enough for it to occur.

That’s a great idea. It may even effect the doppler slightly for a sexier sound. The thing is, the mesh that it should whoosh when hit is a one single mesh, but it has gaps between so it can catch a break when going through. Although I have zero knowledge of coding and I can’t really write such a script.

raycasting and linecasting are pretty simple. If there is a gap / hole in the collider, the ray can go through and not detect anything.

//assuming the layer for your structures is 10

Vector3 head = Camera.main.transform.position;
Vector3 rightEarCheck = head + Camera.main.transform.right * min_distance_for_sound;
RaycastHit hit;
int layerMask = 1 << 10;

if(Physics.Linecast(head, rightEarCheck, out hit, layerMask))
{
    //set a bool true
    //check hit variable for the distance of the hit to control volume / other details of the whoosh
}
else
{
    //set a bool false
}

...

if(boolName)
{
    if(!audio.isPlaying)
    {
        audio.Play();
        audio.volume whatever affected by hit distance
    }
}

–edit

You can even make the audio sources 3D, so you can get the audio right in the player’s ear with the correct direction. Doing so could also take place of adjusting the volume. Just set the audio source position to the hit.point from the linecast and the audio will get more near or far naturally. Here’s a picture :stuck_out_tongue:

1 Like

That’s an awesome setup :smile: I think C sharp scripts needed the public class intro first so I added this :

using UnityEngine;
using System.Collections;

public class WHOOSHAudioCollision : MonoBehaviour {

and closed the caption with } but it gives an error as : Unexpected symbol ‘if’ in class, struct or interface member description. And gives an additional parsing error.

Even if I dont add the monobehaviour it gives errors as : A namespace can only contain namespace declarations and again a parsing error.

That was a chunk of code, not a class. If you’re writing in C# you’ll need to learn the organization of it, heh :slight_smile:

In fact, the chunk isn’t complete either. I set it up to linecast from the right “ear”, the right side of where ever the camera is facing (assumed this game would be first person view). I made too many assumptions for that code to be of any use, but it’s useful to you in seeing how a raycast can be used to solve this problem.

I just realised that if you use raycasting you won’t really get a whooshing sound, but a rather cutout sound. The audio will start the moment raycast hits something and stop when it loses the hitbox, which means it will play and stop the audio instantaneously and not produce a smooth whoosh that fades in and out.

if there is a hit → if play sound is false → set play sound to true, play smooth fade in whoosh
if there is a hit → if play sound is true → play continuous whoosh
if there is no hit → if play sound is true → set play sound to false, play fade out whoosh

You can make use of !audio.isPlaying to stop them from overlapping / interrupting each other.

Can I make a time counter for hitboxes ? That would be the perfect solution.
Like if there is collision, it will start from 0 and increase as long as there is collision. And it will start decreasing back to 0 when there is no hit. There should be a clamp for it so the counter can never go above 1 or below 0. And the counter will define the volume of the looping whoosh sound.

With 3D sounds volume will automatically increase/decrease by distance,
so just need to start playing the looping sound (have the sound on that swoosh object)
when you are getting close enough (for example with that trigger enter)

That means I have to manually place like 300 audio sources :S, aside from that, this many audio running at the same time will definitely go hard on the processor, just doesnt seem like an efficient solution.

Look at the picture I put on the forum, the hand drawn ms paint one lol. You can use just 1 (2 when you have both sides done) and position it where the ray hits. When you have a RaycastHit in your linecast or raycast, it contains a lot of information about what the ray hit.

When ever there is a hit, set the audio source position to the hit.point :slight_smile:

It’s good and well but I have zero knowledge of scripting D: Could you help me out with the script ?

This early in the morning the best I can do is suggest that you read the unity docs for examples of using raycasts and raycasthits. Is your project in C# or JS?

It’s planned to be a PC / android game so both C# and JS should work I think.