• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Vid-Maddness · Jul 26, 2015 at 07:08 PM · gameobjectaudioplaynearis

Play audio when gameobject is near

I need a javascript that plays audio when the enemy is near you and it keeps looping until its a distance away from the player, then when the enemy is far away it stops looping.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by shay4545 · Jul 26, 2015 at 07:35 PM

Well an easy trick would be to just add an audio source to the enemy and play the sound in a loop. You would have to adjust the settings of the audio you are playing and make sure it is a 3d sound. This would make the audio increase as the camera approaches the enemy and decrease as it moves away. The only drawback to this is that multiple enemies close together would play the track multiple times.

Another option would be to use raycasting on from the middle of the camera extending to both sides. When the raycast detects the enemy's layer, you could play the audio.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Vid-Maddness · Jul 27, 2015 at 02:01 AM 0
Share

Is there a way for an audio source on the player to play when the enemy is near?

avatar image
1

Answer by GiyomuGames · Jul 27, 2015 at 06:59 AM

Why don't you just calculate the distance to the enemies? If there is at least 1 enemy under a certain distance you determine then you play your audio clip.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Play audio clip on camera from another gameobject 2 Answers

Audio sound on gameObject delete? 1 Answer

audio source not playing the audio clip 2 Answers

Increase audioclip volume 1 Answer

Can't play any sounds. What's wrong? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges