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 Orggrim · Jan 10, 2013 at 10:03 AM · collisionaudiomove

On Collision, Play Sound If Button Is Pressed

Hi

I was looking to have the game play a walking sound while only on a certain object(s), in this case, a wooden pillar. I wanted to write a script where when the player was on this wooden pillar, the sound would play if he moved around, in this case pressing W.

Heres the script I have so far, feel free to let me know if i was way off or not

If you need any additional info, let me know!

 var StepSound : AudioClip;
 
 function OnCollisonEnter(collision : Collision)
 {
     if (other.tag == "pillar")
         {
             function Update() 
             {
             
                 if (Input.GetKeyDown (KeyCode.W)) 
                     { 
                         audio.clip = StepSound;
                         audio.Play();
                     } 
                 if (Input.GetKeyUp (KeyCode.W))
                     {
                         audio.Stop();
                     }
             }
         }
 }     
Comment
Add comment · Show 1
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 AlucardJay · Jan 10, 2013 at 01:02 PM 0
Share

I wish to clarify : are you asking for a way to play a specific sound based on what type of floor the character is standing on?

Couple of things I have to point out with your script. OnCollisionEnter only happens once, so you are asking if collision just happened and the player pressed W all in the same frame*. That is asking alot, either some amazing powers of prediction or amazing reflexes !

If you want to check for all the time that the object is within the collider, you need to use OnCOllisionStay : http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnCollisionStay.html

I would personally put an arguement in for a raycast. If you programmed it just right, you would only need to raycast in the frame that the footstep sound is played. Which brings me to another point.

Footstep sound i assume is a single sound, I mean that it just goes 'tap' once (not tap-tap-tap). So normally a timer or Invoke function would be used so you could set the ti$$anonymous$$g of the tap-tap-tap so it sounds like a real walking pace. Then you can play with this timer number to change the speed of the tap-tap-tap for running or walking, see =]

There is a script I wrote for that, but it is written for use with the character controller. SO what I suggest is an InvokeRepeating command, that is set to footstepSpeed. then you could just raycast for the type of floor you are standing on, and play the approprate speed and sound all in just a few lines of code. Then you could have a main loop to check if running or walking, and simply CancelInvoke and start InvokeRepeating again with the new time delay.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by cdrandin · Jan 10, 2013 at 10:50 AM

See if this helps http://www.lynda.com/home/Player.aspx?lpk4=109295&playChapter=False @ time 4:40

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 AlucardJay · Jan 10, 2013 at 12:48 PM 0
Share

Hi, that link is for paid subscribers only. Perhaps you could find a free video for everyone to watch, or explain what you were trying to show with that link, thanks.

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

Player should move box, Ai shouldn't 1 Answer

Audio Script 1 Answer

After collision rotate and move 1 Answer

Play Animation OnTriggerEnter 2 Answers

Sound is stopping prematurely (Read Description) 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