Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Play with Single Sound recognizing Obstacles and different Directions

Discussion in 'Audio & Video' started by AnkushAnki, Jun 12, 2015.

  1. AnkushAnki

    AnkushAnki

    Joined:
    Jun 11, 2015
    Posts:
    1
    Hello all,

    I am Developing one Game based on Sounds. I am using single sound. That is for Background. I want that when Player goes near to the Obstacle that background Sound speed will increases. When Player comes more closer to Obstacle then it Speeds up more like a alert to the Player that Obstacle is very near like a Danger sound. How i can do this?

    I also want that Player get to know the Direction or Position of the Obstacle. If Obstacle is on the Right side then Sound in the earphones comes on the Right side and if on Left then Sound will comes on the Left side of earphones.

    Any help will be helpful.

    Thanks in Advance
     
  2. ttww

    ttww

    Joined:
    Nov 9, 2013
    Posts:
    5
    You can control the speed of sound playback (but that also alters the pitch) via the Pitch property of the Audio Source.
    Simply control the pitch from the script based on the current distance to the obstacle, like so:

    Code (CSharp):
    1. var distance = Vector3.Distance(transform.position, player.transform.position);
    2. // minDistance is the distance from the obstacle at which you want the sound to stop speeding up
    3. // maxDistance is the distance from the obstacle at which you want the sound to start speeding up
    4. sound.Pitch = 1 + (distance - minDistance) / (maxDistance - minDistance);
    As for the second question
    I don't really understand the problem, what you describe is called panning and this is built in Unity. You must ensure that Stereo Blend of the Audio Source is set to 3D and your sound will be panned automatically based on the direction of the obstacle relative to the player.