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 GhostDre · Dec 29, 2014 at 09:08 PM · movementsoundwalking

Sound Problem

Hi everyone I'm trying to get a walking sound to play when you move with the game. I tried a lot of methods and the simple audio.clip=walkSound and audio.Play, and non of that worked. Any ideas on how to make the sound play?

     public float walkSpeed = 5.0f;
     public float slowPentaly = 0.5f;
     public float gravity = 20.0f;
     public float run = 2.0f;
     private Vector3 moveDirection = Vector3.zero;
     public AudioClip walkSound;
     
     void Start()
     {
                 //transform.position = new Vector3(0,1.7f,0);
         }
 
     void OnControllerColliderHit(ControllerColliderHit hit)
     {
                 if (hit.gameObject.tag == "Shard") {
                         moveDirection *= slowPentaly;
                 Debug.Log("You are slow");
                 }
         }
     void Update()
     {
                 CharacterController controller = GetComponent<CharacterController> ();
                 if (controller.isGrounded) {
                         moveDirection = new Vector3(Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
                         moveDirection = transform.TransformDirection (moveDirection);
                         moveDirection *= walkSpeed;
 
                             if (Input.GetKeyDown(KeyCode.LeftShift))
                                 moveDirection *= run;
                 }
                 moveDirection.y -= gravity * Time.deltaTime;
                 controller.Move (moveDirection * Time.deltaTime);
 
         }
 
     }

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 YoungDeveloper · Dec 29, 2014 at 09:14 PM

  • You can always use AudioSource.PlayClipAtPoint() http://docs.unity3d.com/ScriptReference/AudioSource.PlayClipAtPoint.html Which will instantiate empty gameObject on it with audio source component with you clip attached, will play automatically and destroy when finished.

  • If the sound is 3d you might not hear if it's far away - isn't positioned correctly.

  • If you are using mecanim you can set animation events and exclude manual step counting.

  • To play the clip you need AudioSource, assign in via inspector or via code source.clip = yourClip, when you want to source.Play();.

  • If you still cant figure it out try it without updates and try triggering the sound from Start().

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 GhostDre · Dec 29, 2014 at 09:45 PM 0
Share

Ok thanks let me try this out.

avatar image GhostDre · Dec 29, 2014 at 10:16 PM 0
Share

Ok so the AudioSource.PlayClipAtPoint plays the sound but it doesnt play right when I move the player. Either the sound keeps looping itself if I put it in the Update function or it plays once in the Start function. Any suggestions??

avatar image YoungDeveloper · Dec 29, 2014 at 11:06 PM 0
Share

It's really impossible to tell without the context and this code. For character controller you should check if isGrounded => activate step counter. You could use coroutine as a separate handler for that.

This is just a quick sketch.

 CharacterController controller;
 bool playerDead = false;
 
 void Start(){
     controller = GetComponent<CharacterController> ();
     StartCoroutine(StepsHandler());
 } 
 
 IEnumerator StepsHandler(){
     float timer = 1f;
     while(!playerDead){
 
         if(timer <= 0 && controller.isGrounded){
             Debug.Log("sound");
             timer = 1f;
         }
 
         else {timer -= Time.deltaTime;}
 
         yield return null;
     }
 }

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Character Movement and Jumping Script? 4 Answers

Walking Motion 1 Answer

[Help] How to prevent sound repeating (in this script) 3 Answers

How to play sound when object stops moving? 1 Answer

Walking using 2 buttons alternately 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