Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Eimantas1 · Nov 20, 2016 at 04:59 PM · javascriptscripting problemarrayraycastingsound effects

Unity 5 Sound repeating when detected by raycast JavaScript

Hello, I have a problem where if a person detects me by raycast he is supposed to stop speaking and then say to go away. Although it is on function update so the raycast detects it 100's of times and plays the sound over and over again. Is there any fix to this WITH raycasting?

(PLAYING THE SOUND)

var speech : AudioClip[];

var inside : boolean = false;

function Update()

{

if (Input.GetKeyDown("e"))

{

GetComponent.().PlayOneShot(speech[0]);

}

if(RaycastingDetection.audioz == true)

{

//GetComponent.().Stop();

GetComponent.().PlayOneShot(speech[1]);

(THE RAYCAST)

static var audioz : boolean = false;

var fwd1 : boolean = false;

function Start()

{

var audio: AudioSource = GetComponent.();

}

function Update()

{

var fwd: Vector3 = transform.TransformDirection(Vector3.forward);

if(Physics.Raycast(transform.position, fwd, 10))

audioz = true;

print ("Raycast Working");

}

Comment
Add comment
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

2 Replies

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

Answer by TheyLeftMe4Dead · Jan 16, 2017 at 04:08 AM

I'm a bit confused with your format but think I can help. (Usually you would make separate functions for Update to call for readability.) The main issue I see is that you are checking if "RaycastingDetection.audioz == true" in the update function. If a person's ray is detecting you it will return true. Therefore if you keep standing in front of them, RaycastingDetection.audioz will stay true.
Because RaycastingDetection.audioz will stay true, and the Update function checks if it is true every frame, the audio will play continuously.

To fix this, I would recommend that you create a separate function called WaitForAudio(). You would call this after the person's audio "Go Away" starts to run. I would see it like this:

 function WaitForAudio() {
      GoAwayPlaying = true;
      Yield WaitForSeconds(GoAway.length);
      GoAwayPlaying = false;
 }

Then, change your if statment like this:
If (RaycastingDetection.audioz == true && GoAwayPlaying = false) Play Audio; WaitForAudio();

Comment
Add comment · 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
0

Answer by Eimantas1 · Jan 17, 2017 at 11:26 AM

Thanks for the answer, this has been asked a while ago so I already found out a solution. My solution is to check if the audio source is playing by using "!" and if it is not then it plays a one shot sound. But thanks for the answer.

Comment
Add comment · 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

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

129 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 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 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 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 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

need help in randomising array 1 Answer

Unknown Identifier : Hunger? 1 Answer

I Need help with a music cycle generator in C# 0 Answers

Make gunshot audio continue to play even when projectile is deleted onCollion 0 Answers

Java Script to talk to C# script 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