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 Besi2019 · Apr 14, 2018 at 09:00 PM · triggerobjectsoundsound effects

Object Sound Trigger

I've made an axe which is swinging from right to left and from left to right, but now I want to add a sound to it. I want that when the axe hits the middle of the swing, that it should play the sound, which is a "woosch" sound effect.

Here is my script:

 public Rigidbody2D body2d;
 public float leftPushRange;
 public float rightPushRange;
 public float velocityThreshold;

 // Use this for initialization
 void Start () {
     body2d = GetComponent<Rigidbody2D>();
     body2d.angularVelocity = velocityThreshold;
 }
 
 // Update is called once per frame
 void Update () {
     Push();
 }
 
 public void Push()
 {
     if (transform.rotation.z > 0
         && transform.rotation.z < rightPushRange
         && (body2d.angularVelocity > 0)
         && body2d.angularVelocity < velocityThreshold)
     {
         body2d.angularVelocity = velocityThreshold;
     }
     else if (transform.rotation.z < 0
         && transform.rotation.z > leftPushRange
         && (body2d.angularVelocity < 0)
         && body2d.angularVelocity > velocityThreshold * -1)
     {
         body2d.angularVelocity = velocityThreshold * -1;
     }
 }

How to do that?

Comment
Add comment · Show 3
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 shadowpuppet · Apr 15, 2018 at 07:43 PM 0
Share

deter$$anonymous$$e how many frames into the swing before you want the whoosh sound and use a coroutine?

 public AudioClip whoosh;
 public float whooshDelay;//time waiting for middle of swing;
 
 //so in your script wherever it is you are starting the swing add:
 StartCoroutine(DelayBeforeWhoosh());
 
 private IEnumerator DelayBeforeWhoosh(){
         yield return new WaitForSeconds(whooshDelay);
 audio.PlayOneShot (whoosh);
     }
 }
avatar image Besi2019 · Apr 16, 2018 at 04:03 PM 0
Share

@shadowpuppet

I doesnt work and how do I know how many frames it has, when I did it with a hinge joint?

avatar image shadowpuppet Besi2019 · Apr 16, 2018 at 04:35 PM 0
Share

as for the frame #, just guess at first with a float of some value, say .5f. If it's too soon raise the value to .7f, if it's to late, lower the value . 3f. Adjust as needed til it's when you want it. As far as not working goes...nothing in that simple example to prevent it from working so the coroutine simply must not be initiating. add a Debug line in the coroutine to see if that's the problem. If you put the StartCoroutine in the spot that triggers the axe animation then it should work

Debug.Log ("coroutineWorking"); are there any error messages?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Besi2019 · Apr 15, 2018 at 10:07 AM

@James9270_

I thought I would add you to this question also, since you were so good at sound effect scripts.

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

96 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

Related Questions

Trigger sound on another gameobject 1 Answer

Sound on keypress, not working 2 Answers

How to get sound to play on barrel Explosion 1 Answer

Need help making an audio trigger. 2 Answers

How do I move an object from point A to point B using a trigger? 3 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