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 phavington · Aug 01, 2012 at 07:21 AM · soundplayspotlightflashlighthover

How to make a sound play when my flashlight (spotlight) hits an object

I would like to make a sound play when a spotlight (acting as a flashlight) hits an object. I tried making it with a hover function, but for some reason it didn't work. Is there any other way?

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 Tommynator · Aug 01, 2012 at 07:28 AM 0
Share

You could use Physics.Raycast() and use layers or tags to decide if the object that was hit should play a sound.

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by fafase · Aug 01, 2012 at 07:27 AM

Add a raycast to your light.Put this script on your light object.

 var sound:AudioClip;
 
 function Start(){
    audio.clip = sound;
 }
 function Update () {
     var hit:RaycastHit
     if (Physics.Raycast (transform.position, transform.forward,hit, 10)) {
         if(hit.collider.gameObject.tag=="ObjectTagged"){
             audio.Play()
         }
     }
 }

This will throw an invisible ray in front of your light for 10m. If the ray hits an object it checks the tag of the object and if it matches then it plays the sound loaded.

If you want to see the actual ray in the Scene view (you cannot see it in the game view), you can use:

 Debug.DrawLine (transform.position, transform.forward, Color.red);
Comment
Add comment · Show 7 · 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 phavington · Aug 01, 2012 at 06:25 PM 0
Share

I get some errors:

Assets/Scripts/heartbeat.js(7,64): BCE0005: $$anonymous$$ identifier: 'hit'.

Assets/Scripts/heartbeat.js(8,12): BCE0005: $$anonymous$$ identifier: 'hit'.

avatar image thellama · Aug 01, 2012 at 07:03 PM 1
Share

Add:

var hit : RaycastHit;

after :

function Update(){

avatar image phavington · Aug 01, 2012 at 07:38 PM 0
Share

It works! Thanks

avatar image blackpantherxx · Sep 23, 2012 at 03:38 PM 0
Share

i use this but nothing happends:

var sound:AudioClip;

function Start(){ audio.clip = sound; } function Update () { var hit:RaycastHit; if (Physics.Raycast (transform.position, transform.forward,hit, 10)) { if(hit.collider.gameObject.tag=="Enemy"){ audio.Play(); } } }

avatar image fafase · Sep 24, 2012 at 04:10 AM 0
Share

Is your enemy tagged as "Enemy", not named.

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

10 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

Related Questions

Play a sound when flicker? 1 Answer

Audio Playing upon death 1 Answer

My Draw Calls go from 30 to 125 and I have twice as much tris/verts when I enable a spotlight. Why?? 1 Answer

How To play sound with a keypress 2 Answers

How to Play a Sound from an Array from an Overloaded Method? 1 Answer


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