Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by BLOODragonGames · Dec 04, 2012 at 02:59 AM · c#audioenemy

How do you make loop sounds exclusive to distance in C#?

In my game, when the enemy gets to a certian distance, audio (for an example, the sound is called "bread") will be played/looped. When he is farther away (or you move farther away) the audio will stop playing (but will still be true to play when the enemy gets close again to you). As you progress (let's say, you collected 3 "bacon strips" [for an example]) the distance needed to play the audio will be longer than when you were at one.

Can someone please give me an example using those listed above (I ask only so I can figure where to put "audio" and where to put "bread")?

Comment
Add comment · Show 2
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 ThePunisher · Dec 04, 2012 at 06:30 AM 0
Share

I'm so confused, is there a technical question you are asking? What do you mean exclusive to distance?

avatar image BLOODragonGames · Dec 05, 2012 at 02:16 AM 0
Share

The question was how do I get looped audio to only play when the enemy was to a certian distance. "Bread" is an example of the title of the looped audio name. "Bacon strips" is an example on what you're goal is to collect, and how the distance of the audio changes on how much you've gathered.

$$anonymous$$ake sense?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by fafase · Dec 04, 2012 at 08:04 AM

First on the object that gets the distance with the player

ObjectDistance.cs

 public float distSqr=10;
 Transform player;
 Transform _transform;
 
 void Start(){
    player =GameObject.Find("Player").transform;
    _transform = GetComponent<Transform>();
 }
 
 void Update(){
    float dist = (player.position - _transform.position).sqrMagnitude;
    if(dist<distSqr){
       if(!audio.isPlaying)
          audio.Play();
    }else if(audio.isPlaying) audio.Stop();
 } 

Now on the player:

 int bread=0;
 ObjectDistance script;
 
 void Start(){
    script =GameObject.Find("DistanceObject").GetComponent<ObjectDistance>();
 }
 
 void OnTriggerEnter(Collider col){
    if(col.gameObject.tag =="Bread"){
       script.distSqr += 10;
       bread +=1;
    }
 }

Now for explanations, the first script is attached to the object that plays the sound. It checks the distance with the player. I use sqrMagnitude because it is cheaper than Distance which involves sqr root. So consider that all distances are squared, so 10 is actually 3.33333m.

If the dist is smaller than the define distance then we check if the audio is not already playing and if not then we play. If the dist is greater then the audio is stopped or finally nothing happens.

The player has the second script. First we get a reference to the script of the sound object. Then the bread has a trigger collider and if we enter it then we go to add 10 to the sqrDistance (so 3.33333m more in real) and we increase the amount of bread.

Hopefully it works.

Comment
Add comment · Show 14 · 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 BLOODragonGames · Dec 05, 2012 at 04:35 PM 0
Share

Question, do I attach the audio to the player or the enemy?

avatar image fafase · Dec 06, 2012 at 08:15 AM 0
Share

This is supposed to play on the enemy since the audio.Play() is on the enemy script. The player script checks how many breads you have and reports to the enemy the amount so that the enemy script checks a larger distance.

avatar image BLOODragonGames · Dec 06, 2012 at 11:03 PM 0
Share

It says 'ObjectDistance' could not be found (7,9) on the Player's script. It's confused and thinks I'm missing a directive or an assembly reference.

avatar image BLOODragonGames · Dec 07, 2012 at 11:53 PM 0
Share

It worked. There's an issue with 'distSqr' though. It says there's no deffinition for it in the Player's script

avatar image fafase · Dec 08, 2012 at 08:05 AM 0
Share
 script =GameObject.Find("DistanceObject").GetComponent<ObjectDistance>();

This is the line creating a reference to the other script in the player script.

 script.distSqr

is how you access. Have you modified both names "DistanceObject"(1) and "ObjectDistance"(2) to the appropriate?1 is the object name 2 is the script name

Show more comments

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.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[C#] Enemy flys at Player 0 Answers

Sounds triggering too fast 4 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Simple enemy kill count 1 Answer

How to make my enemy stop moving once dead? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges