0
\$\begingroup\$

I need to make the sound of a bullet crossing next to player, I tried by placing a hidden cube and make it a trigger, and when the bullet enters it, a sound must be played, but it didn't work because the bullet is very fast.

And I don't want to do that by firing a Ray, so is there a way to do that?

New contributor
Ahmed Dyaa is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
\$\endgroup\$
6
  • 1
    \$\begingroup\$ What is your goal? To let the player hear the bullet? To have the sound correctly in a surround sound system? To have a realistic sound of the bullet traveling through the air? To have a variance of sound based on distance? To have a single sound or multiple at once? \$\endgroup\$
    – Zibelas
    May 22 at 12:13
  • \$\begingroup\$ I want the player to hear a 3D "whip / the sound of the bullet traveling through air" sound when the bullet passes by him. \$\endgroup\$
    – Ahmed Dyaa
    May 22 at 13:24
  • \$\begingroup\$ For example, when the bullet touches the hidden cube, a game object is instantiated, contains the Audio source, and plays the clip. \$\endgroup\$
    – Ahmed Dyaa
    May 22 at 13:36
  • 2
    \$\begingroup\$ Why do you not want to do it by firing a ray? \$\endgroup\$
    – DMGregory
    May 22 at 13:49
  • \$\begingroup\$ How will it know the point at which the gameObject will be instantiated to play the sound? And if this was done by placing, for example, a hidden collider, and the ray collided with it, and the point was known, and the sound was played How will the sound be played again if passing by another player? \$\endgroup\$
    – Ahmed Dyaa
    May 22 at 14:32

1 Answer 1

0
\$\begingroup\$

How you do this will depend on the type of sound effect you're trying to play.

If the bullet sound effect will be a continuous effect, such as a zipping/whooshing sound, one simple way to do this is to put an AudioSource on the bullet with a small radius, and have it loop the sound effect. Then the player will only hear the effect if they are close to the bullet, and the positioning of the audio will always be correct.

If the sound effect will be a sharp crack or something similar that doesn't loop well, or the bullet moves too fast to hear a sound effect that's attached to the bullet, you'll need to use something like raycasts to determine if the sound effect should play and where it should play from.

If you use a raycast, the general process would look like this:

public class BulletSoundPlayer : MonoBehaviour {
    [SerializeField] private AudioClip audioClip;
    [SerializeField] private LayerMask layerMask;

    public void DoCast(Vector3 origin, Vector3 direction) {
        if (Physics.Raycast(origin, direction, out var hit, Mathf.Infinity, layerMask)) {
            //you'll need to change this line to look for a component or tag that you use
            if (!hit.collider.CompareTag("PlayerBulletAudioZone")) return;

            //you might replace this with code that spawns a pooled 
            //AudioSource instance at the given point
            AudioSource.PlayClipAtPoint(audioClip, hit.point);
        }
    }
}

Here we check if the bullet's path intersects a collider with the tag "PlayerBulletAudioZone". If so, we play the audio clip at the intersection point. This approach is very simple and won't always give the most realistic results, but should be a good enough start.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .