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 /
avatar image
0
Question by DexiTheZombie · Jan 30, 2018 at 02:03 PM · transformaudioaudiosourceeulerangleslever

VR Lever to play sound between two values doesnt work ,

Hello im kinda new to unity and am making a TardisVR game however im now setting up the scripts for my levers to play sound im using the below script to play a sound when the lever rot is between 85 and 90 but being new i dont think ive wrote it right

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MatSound : MonoBehaviour {
 
     public AudioClip TardisClip;
 
     public AudioSource TardisSource;
 
     // Use this for initialization
     void Start () {
         TardisSource.clip = TardisClip;
     }
     
     // Update is called once per frame
     void Update () {
         if (gameObject.transform.localEulerAngles.x >= 80 && gameObject.transform.localEulerAngles.x <= 90) {
                 TardisSource.Play ();
         }
     }
 }

The assets im using are Oculus SDK VRTK Toolkit please if anyone can help i would really appreciate it

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

Answer by Legend_Bacon · Jan 30, 2018 at 03:36 PM

Hello there,

You could probably start by adding Debug.Log("X = " + transform.localEulerAngles.x.ToString()); in your update, just to make sure you are indeed between the correct values.

Other than that, be careful about playing an audio source on Update(). It means that it will try playing the sound every frame (60 times a second). This might also be your problem. I would recommend using something like THIS to make sure that doesn't happen.


I hope that helps!

Cheers,

~LegendBacon

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 DexiTheZombie · Jan 30, 2018 at 03:59 PM

@Legend_Bacon Hey yea ive got it working now apart from when the lever is no longer in those values it instantly stops the audio, is there a way where i can make it so when the lever has entered the value range it starts when even if the lever moves out of that range it doesnt stop and also if the lever were to enter that range mid audio playing that it wouldnt play it again ? sorry if this is a lot

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 Legend_Bacon · Jan 31, 2018 at 10:35 AM 0
Share

Hello there,

No worries, I'm here to help. The link I sent you above should have been the right solution, but maybe something went wrong. Could you post your code again? If by any chance you put an ELSE statement saying TardisSource.Stop (), then that would definitely be the cause.

Cheers,

~LegendBacon

avatar image DexiTheZombie Legend_Bacon · Feb 02, 2018 at 01:45 PM 0
Share
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class LeverSoundTesting : $$anonymous$$onoBehaviour {
 
     AudioSource TardisSource;
     public AudioClip TardisClip;
 
     // Use this for initialization
     void Start () {
         TardisSource = GetComponent<AudioSource>();
     }
     
     // Update is called once per frame
     void Update () {
         if (!TardisSource.isPlaying)
         {
             TardisSource.clip = TardisClip;
             TardisSource.Play();
         }
     }
 }
 

Now what happens is it just instantly plays the clip

avatar image Legend_Bacon DexiTheZombie · Feb 03, 2018 at 01:27 PM 0
Share

Ah, I see. Sorry, I might not have been very clear before.

What I meant to tell is that you need to ADD this solution to your existing code, not REPLACE it.

In the code above, every frame (60 times a second), your script checks if the sound is playing. And if it isn't, then it plays it. What you actually want to have is the check you originally had:


      if (gameObject.transform.localEulerAngles.x >= 80 && gameObject.transform.localEulerAngles.x <= 90) 
 {
       if (!TardisSource.isPlaying)
      {
          TardisSource.clip = TardisClip;
          TardisSource.Play();
      }
  }



Now, this assumes that your lever will NOT fit the angle conditions by the time the sound is not playing anymore. Otherwise, it might play multiple times as long as the conditions are true. You could implement a bool check, eg "hasPlayedSound", to fix this. It all depends on the behaviour you would like for your game.

I hope that helps!

Cheers,

~LegendBacon

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

109 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

Related Questions

Audio not playing... 2 Answers

Can't get sound clip from array to play properly 1 Answer

The same audio plays differently on a variety of buttons, how can I change this? 0 Answers

Audio Clip Array within an Audio Source Array 0 Answers

PlayOneShot - skips and doubles the sound. 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