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 Sir_Paddles · Jan 28, 2013 at 01:45 AM · collisionaudio

My first script, and I need some help.

Made a previous post concerning playing audio on collision, and ended up with this code.. #pragma strict

 function Start () {
 
 var play;
     if pause = true 
         function OnCollisionEnter(collision : Collision) {
     // Debug-draw all contact points and normals
     for (var contact : ContactPoint in collision.contacts) {
         Debug.DrawRay(contact.point, contact.normal, Color.white);
     }
     
     // Play a sound if the coliding objects had a big impact.        
     if (collision.relativeVelocity.magnitude > 2)
         audio.Play();
 }
 
 var pause;
         if play = true
         function OnCollisionEnter(collision : Collision) {
     // Debug-draw all contact points and normals
     for (var contact : ContactPoint in collision.contacts) {
         Debug.DrawRay(contact.point, contact.normal, Color.white);
     }
     
     // Play a sound if the coliding objects had a big impact.        
     if (collision.relativeVelocity.magnitude > 2)
         audio.Pause();
 }
 
 
 }
 
 function Update () {
 
 }

I know there's stuff wrong with it, but could someone just blunty tell me or fix it so I can learn from a side by side comparison? I'm not very good at learning in bits and pieces.

Thans - Sir Paddles of Loxely

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 Sir_Paddles · Jan 28, 2013 at 12:41 AM 0
Share

I forgot to say what I want it to do.

I want the object to play/pause music when the player runs into it, or it is collided with in general.

avatar image Hyperion · Jan 28, 2013 at 04:23 AM 0
Share

I just want to say that the way I use sounds, and it works well, is by making a variable of AudioSource, and then I do AudioSource.PlayClipAtPoint(yourSound). At least something like that. But that's for playing a sound. So in your case, do you have an audio source?

1 Reply

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

Answer by robertbu · Jan 28, 2013 at 05:50 AM

Here is a rewrite of your script.

 var playing = false;
 
 function OnCollisionEnter(collision : Collision)
 {
     // Debug-draw all contact points and normals
     for (var contact : ContactPoint in collision.contacts) {
         Debug.DrawRay(contact.point, contact.normal, Color.white);
     }
 
 // Play/pause a sound if the colliding objects had a big impact.        
     if (collision.relativeVelocity.magnitude > 2) {
         if (playing)
                 audio.Pause();
            else
                audio.Play();
                
            playing = !playing; // flips value
     }
 }

A couple pointers. You cannot have the same function twice in a script as you have OnCollisionEnter(). You shouldn't nest functions as you have with your OnCollisionEnter's inside of the Start() function. You should lookup the syntax of the if statment and also take a look at how elements are compared. The '=' by itself is for assignment not comparison.

Comment
Add comment · Show 1 · 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 Sir_Paddles · Jan 28, 2013 at 12:56 PM 0
Share

Ah thank you very much. It's a lot easier to figure this stuff out with a right and a (extremely) wrong to look at, thank you. I obviously still have much learning to do.

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

On Collision, Play Sound If Button Is Pressed 1 Answer

help sound on collision when key down 2 Answers

OnTriggerExit stop audio 1 Answer

How to play an audio one shot clip on a collision 1 Answer

Audio on Collision Issue 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