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 Oobi23 · Jun 05, 2013 at 07:11 PM · raycastsoundpush

How to use sound effects for push

Hi there guys, So basically here is my front movement script, but I am unsure of how to write up a script that would make a sound work for the push that is used in this script. I have already imported the sound to unity and made a variable of it accordingly..(var pushSound : AudioClip;) how do I proceed further to make the sound work when I push the box?

here is the coding so far..

 if (Input.GetKeyDown(KeyCode.UpArrow)) {
 
 
  if (Physics.Raycast (transform.position, fwd, hit,1)) {
        print ("There is something in front of the object!");
             if(hit.collider.gameObject.tag!="Push"
             )
              {
     if (hit.collider.gameObject.tag == "endGame"){
                 Application.LoadLevel(0);
             }else{
                 transform.position += Vector3.up;
                 //animation.CrossFade("DefaultAnim", 0.2);
             }
     
             }
             else{
                    print ("Push!");
                     var pushHit1:RaycastHit;
                     if (!Physics.Raycast (hit.collider.gameObject.transform.position, fwd, pushHit1,1)) {
                   hit.collider.gameObject.transform.position += Vector3.forward;
                    transform.position += Vector3.forward;
                    audio.PlayOneShot(pushSound, 0.7);
                    
                     }
                 
             }
             
     }else{
     Debug.Log("nothing in front");
     

   
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by KiraSensei · Jun 05, 2013 at 07:52 PM

Attach the component AudioSound to the object to push (with the sound to be played, of course).

Then after moving your object, play the sound :

 hit.GetComponent(AudioSource).Play();

ps : I think your hit variable is a game object ? In that case the line above is good, and you can simplify your code :

 hit.transform.position += Vector3.forward;

And

 if(hit.tag!="Push")

Instead of

if(hit.collider.gameObject.tag!="Push")

Comment
Add comment · Show 2 · 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 Oobi23 · Jun 05, 2013 at 08:15 PM 0
Share

that does simplify my code and makes it look neater.. btw.. where do I exactly put=

 hit.GetComponent(AudioSource).Play();
avatar image KiraSensei · Jun 05, 2013 at 08:20 PM 0
Share

After

 hit.transform.position += Vector3.forward;

for example

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

15 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

Related Questions

Advance when the sound is done playing 1 Answer

Multiple/Different footsteps sounds (solved) 2 Answers

Sound help with script 1 Answer

help with multiple sounds 0 Answers

can foe hear us? 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