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 $$anonymous$$ · Aug 14, 2013 at 05:50 PM · raycastaudiorendererplayvisible

How do I use RayCasting to play a sound when my player is close to an enemy

I want to have a sound play when my player is close to an enemy AND only when that enemy is visible ( i'll be using renderer.isVisible ) If my enemy comes up behind me and i turn around a sound doesn't play . I tried used trigger zones but i couldn't get that to work, and i'm not very familiar with raycasting so help is appreciated. (could it be possible to have a trigger on the player so when the enemy entered it and was visible it played? how could I refer the enemy to the render.isVisible)

my current script function OnTriggerEnter(other : Collider){ if(other.gameObject.tag == "Player" && renderer.isVisible){ audio.Play(); } }

FOR ANYONE INTERESTED FINAL SCRIPT IS

function OnTriggerStay(other : Collider){

     if(other.gameObject.tag == "Player" && renderer.isVisible && !audio.isPlaying){
    
    audio.Play(); }}
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 DaveA · Aug 14, 2013 at 05:52 PM 0
Share

If by 'trigger zones' you mean using a Collider with 'istrigger' set, that's the easiest, so I would be interested in what you did there and why that didn't work for you.

avatar image $$anonymous$$ · Aug 14, 2013 at 05:56 PM 0
Share
 function OnTriggerEnter(other : Collider){
 
 if(other.gameObject.tag == "Player" && renderer.isVisible){
 audio.Play();
 }
 }


the sound plays if i'm looking at him and if i walk into the zone, however I want it to play if he comes up behind me and I turn around. Sorry if i explained it badly ;o

1 Reply

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

Answer by brandonetter · Aug 14, 2013 at 06:13 PM

Ah, I haven't tested this but have you tried changing "OnTriggerEnter" to "OnTriggerStay"? I think your issue is that your 'Player' enters the collider while the renderer isn't visible, and when the renderer IS visible you aren't entering the collider, you're already in it.

    function OnTriggerStay(other : Collider){
          
         if(other.gameObject.tag == "Player" && renderer.isVisible){
         audio.Play();
         }
         }
 
Comment
Add comment · Show 5 · 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 $$anonymous$$ · Aug 14, 2013 at 06:22 PM 0
Share

Ahh this seems to be working a lot better now thank you =D <3, do you know the script to not play audio if it's already playing?

avatar image brandonetter · Aug 14, 2013 at 06:25 PM 0
Share

Oh... it it just continuing to play over and over? Ha sorry about that. try this-

http://docs.unity3d.com/Documentation/ScriptReference/AudioSource-isPlaying.html

avatar image $$anonymous$$ · Aug 14, 2013 at 06:32 PM 0
Share

Yeah my fault really haha =D basically it just kept starting the clip again half way through if I moved. Thank you very much for your help =D

avatar image brandonetter · Aug 14, 2013 at 06:32 PM 0
Share

Oh, and is there some kind of horror game-jam going on? So many questions about this kind of thing today. Best of luck btw!!

avatar image $$anonymous$$ · Aug 14, 2013 at 06:34 PM 0
Share

xD well unity is a very popular engine for horror after slender, I always wanted to make games and this is my first but i really have fun with horror especially =D

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

16 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

Related Questions

Audio clip not playing 1 Answer

Guys i need help with audio! 0 Answers

Problem changing renderer back to original after changing it on raycast hit 1 Answer

Choose from 2 audio Sources 2 Answers

Trying to play other objects' sound and animations from Raycast hit 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