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 swooshgames · Aug 18, 2020 at 11:55 AM · updateupdate functionupdate problem

Help with sound playing when a certain bool = false

I am trying to make the footstep sound play when the player's speed = 3f (3f is sprinting speed) . For that though i have to check whether or not the player is sprinting by calling it from the Update()function.


Now if i call it from there? It'l run every frame which will not give the sound enough time to finish playing. So i created a bool: playedSoundRunand put it in an if statement

 if (playedSoundRun == false)
 {
     GrassRunSound();
 }

if set to false it will continue to check the GrassRunSound() statement every frame. I also created another bool soundRunCheck to check if the speed = 3f


The sound works when i'm running, the problem is just that when the speed goes back to 1, the GrassRunSound() still continues to play. If someone could help me figure out whats wrong with my code that would be amazing.


Here's my whole script (Ignore the GrassWalkSound() function for now:

 public CharacterController charController;

 public float speed = 12f;
 public float gravity = -9.81f;
 public float jumpHeight = 3f;

 public Transform groundCheck;
 public float groundDistance = 0.4f;
 public LayerMask groundMask;

 private Vector3 velocity;
 private bool isGrounded;

 public AudioSource grassWalk;
 public AudioSource grassRun;

 private bool playedSoundRun = false;
 private bool soundRunCheck = false;

 void Start()
 {
     playedSoundRun = grassRun.isPlaying;


     grassWalk.Stop();
 }
 
 void Update()
 {
     isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

     if (isGrounded && velocity.y < 0)
     {
         velocity.y = -2f;
     }

     float x = Input.GetAxis("Horizontal");
     float z = Input.GetAxis("Vertical");

     Vector3 move = transform.right * x + transform.forward * z;

     charController.Move(move * speed * Time.deltaTime);

     velocity.y += gravity * Time.deltaTime;

     charController.Move(velocity * Time.deltaTime);

     if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
     {
         velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
     }

     if (playedSoundRun == false)
     {
         GrassRunSound();
     }


     CharacterSprint();
 }

 private void CharacterSprint()
 {
     if (isGrounded && Input.GetKeyDown(KeyCode.LeftShift) && Input.GetKey(KeyCode.Z))
     {
         soundRunCheck = true;
         speed = speed + 2f;
     }

     if (Input.GetKeyUp(KeyCode.LeftShift))
     {
         soundRunCheck = false;
         speed = 1f;
     }

     if (Input.GetKey(KeyCode.LeftShift) == false && Input.GetKey(KeyCode.Z))
     {
         soundRunCheck = false;
         speed = 1f;
     }
 } 

 void OnCollisionEnter(Collision collision)
 {
     if (collision.gameObject.CompareTag("Water"))
     {
         gravity = gravity - 10f;
         speed = speed - 1f;
     }

     if (collision.gameObject.tag == "Water")
     {
         gravity = -17;
         speed = 1f;
     }

 }

 void GrassWalkSound()
 {

 }

 void GrassRunSound()
 {
     if (isGrounded && soundRunCheck == true)
     {
         grassRun.Play();
         grassRun.loop = true;
         playedSoundRun = true;
         
     }
     if (soundRunCheck == false)
     {
         grassRun.Stop();
         playedSoundRun = false;
     }
 }

}

PS: I know all the if statements makes it seem a little noob-ish, i'm a beginner

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 rh_galaxy · Aug 19, 2020 at 12:32 AM

GrassRunSound() is never called again when playedSoundRun is set to true, so grassRun.Stop() will never be called...

   if (soundRunCheck == false)
   {
     grassRun.Stop();
     playedSoundRun = false;
   }

needs to run before CharacterSprint(), I think. Move that block from GrassRunSound.

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

136 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 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

Switch statement not running on every frame 1 Answer

How to call PlayerPrefs in an Update function without hogging memory? 0 Answers

refactoring functions which *must* be in Update() 0 Answers

update method isnt working 0 Answers

How do I update an image to all connected Players on a Multiplayer server? 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