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 simstosh · Jan 09, 2012 at 04:03 PM · followsounds

Making an NPC/Object follow a Sound

I'm making a research about the Unity and other engine resources, and I want to know if is it possible to make, for example, an NPC or another object follow a sound. For example, if I throw the stone into an floor, she makes a noise, and If exists a object in an circular range, this object moves to the stone.

Is it possible to make something like that?

Comment
Add comment · Show 1
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 simstosh · Jan 09, 2012 at 03:08 PM 0
Share

For example: The Object (follower) stays at position [0 0 0] (X Y Z) and listen to all "sounds" next to him (by a range), if I throw a stone (who makes a noise) on position [20 0 0], the "follower" moves at the stone position [20 0 0]. But the "follower" only moves if he "listen" this sound, not if the object pass next to his range (not Physics.Raycast, for example).

Sorry if I cannot explain right. :/

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Julien-Lynge · Jan 09, 2012 at 04:46 PM

The easiest way to have objects move towards a sound is to keep the objects that are 'listening' in a catalog and let the catalog know every time a sound is produced. To register your objects, set up a script with something like the following (C#), where MyScript is the (non-static) class that moves the GameObjects towards the sound. Please note that this is psuedo-code and hasn't been tested.

 (catalog object)
 MyCatalog.cs
 List<Transform> listenerTransforms = new List<Transform>();
 
 public void registerListener(MyScript script)
 {
     listenerTransforms.add(script.transform);
 }
 
 public void checkListenersAgainstSound(Vector3 position)
 {
     foreach(Transform listener in listenerTransforms)
         if (Vector3.Distance(position, listener.position < someValue))
             listener.GetComponent<MyScript>().moveTowardsSount(Vector3 position);
 }
 
 (listener)
 MyScript.cs
 void Start()
 {
     FindObjectOfType(typeof(MyCatalog)).registerListener(this);
 }
 
 (object producing the sound)
 SoundProducer.cs
 MyCatalog catalog;
 void Start()
 {
     catalog = FindObjectOfType(typeof(MyCatalog));
 }
 
 void produceSound()
 {
     //start playing the sound
     [code]
 
     //let the catalog know to check against this sound
     catalog.checkListenersAgainstSound(transform.position)
 }
Comment
Add comment · Show 2 · 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 simstosh · Jan 09, 2012 at 05:38 PM 0
Share

Thanks, I will study this code later (when I get off the work)

I don't want to abuse, but do you have a example like this, but in Javascript? :P

avatar image Julien-Lynge · Jan 09, 2012 at 06:37 PM 0
Share

hrm... Offhand, most things should be the same, except change the methods (void produceSound() -> function produceSound() : void) and change the variable declarations ($$anonymous$$yCatalog catalog; -> var catalog : $$anonymous$$yCatalog). I really don't work in JS much, so there may be other things. Take a look at this link: http://fragileearthstudios.com/2011/10/18/unity-converting-between-c-and-javascript-2/

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Camera rotation around player while following. 6 Answers

Why is my character in the Mecanim Follow Example scene floating? 0 Answers

Camera Follow sphere with ridgidbody? 4 Answers

object on a spline following another object(superspline) 0 Answers

Moving while facing the camera 0 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