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 Pflobus · Dec 19, 2014 at 02:04 AM · gui

FirstPerson and Audioplay???

So, I am working on a first person museum-esque project. My problem is that I would like it so that whenever there is a specific sphere, the player can mouse over it, it'll get highlighted, and then a certain audioclip will play. Any ideas? Any would be appreciated :)

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 HYPERSAVV · Dec 19, 2014 at 02:16 AM 0
Share

Try to break down your problem into individual steps.

  • The player looks at something (Raycast)

  • Object hit by raycast is tagged as "objectThatShouldPlayAudio"

  • Tell that object to "highlight" itself. (This can be done many ways, either manipulating it from your controlling script, or defining a public method in a script attached to the object which you call)

  • Tell that object to start playing its clip. (Again, can be done many ways. You could define every known clip in your controlling script and play those, or you can have audio clips referenced in a script attached to the object. Similar to before, you can call a method attached to the object that tells it to start playing its clip)

avatar image Pflobus · Dec 19, 2014 at 02:26 AM 0
Share

Any ideas on how to script that? Also, is there an existing script to teleport you when you impact an empty gameobject, to go to another scene?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by HYPERSAVV · Dec 19, 2014 at 02:43 AM

Bit of a disclaimer: I am at work, will not be able to compile this. Treat this as pseudo-code.

This is what you would use to tell that you've "looked" at an object.

 void FixedUpdate()
 {
    RaycastHit2D hit = Phsyics2D.Raycast(transform.position, transform.forward);
    
    // This assumes you have set up the tags appropriately
    if(hit.collider.tag == "NoiseyObject")
    {
       hit.gameObject.GetComponent<NoiseyObjectScript>().Highlight();
       hit.gameObject.GetComponent<NoiseyObjectScript>().PlayNoise();
    }
 }


Elsewhere, attached to your objects that make noise, you have a script attached that defines the following:

 public void Highlight()
 {
    // "Highlight" the object
 }
 
 public void PlayNoise()
 {
    // audio is an AudioSource you've defined elsewhere
    if(!audio.isPlaying())
    {
       audio.Play();
    }
 }

Keep in mind that these will be called for each Update cycle, which is why we do a check to see if audio has started already. You could add a flag within the second script to say "hey I'm being looked at right now" that your first script can query, and if so move on.

Comment
Add comment · Show 3 · 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 Pflobus · Dec 19, 2014 at 03:14 AM 0
Share

2 things I am confused about: (sorry to be needy :/ )

  1. When you say: //highlight the object

Would I add the void FixedUpdate there?

2 . Is there a way to put this all into one script (with vars for the gameobject and audio) ?

Thanks for your help!

avatar image HYPERSAVV · Dec 19, 2014 at 09:03 PM 0
Share

I'm not sure what you want to do for highlighting an object, so that's where the logic would go to do that. If you want to change the color, add a glow to it, yada yada, you would do that there.

I don't see why you couldn't put this all in one script, but I personally would not. An object that knows how to highlight itself should implement that logic, and another object should tell it to do it, not how to do it.

If you felt tempted to make it one script, you would need to pass a reference to the object into the method like so

 public void HighlightObject(GameObject gob)
 {
    // Highlight object
 }
 

This way you would perform the logic of highlighting an object on an object passed in as a parameter (ins$$anonymous$$d of the implied "this" if it were just attached as a separate script).

avatar image Pflobus · Dec 20, 2014 at 03:16 PM 0
Share

Thanks. Oh, and I like your new username.

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

27 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity 2d mobile android GUI texture button 1 Answer

ammo count help 1 Answer

Why is networkbehaviour underlined in green? 1 Answer

Writing in text box without selecting it? 1 Answer

Alpha cutoff for a GUI Texture? 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