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 Fuzzy_Puppy · Jul 04, 2021 at 01:57 AM · sound effects

Sound Effect Timing

im tryna make a sound play as megaman lands after jumping. this is my code, but the sound always plays immediately after he jumps, rather than when he hits the ground. what am i missing here?

     private void FixedUpdate()
     {
         isGrounded = false;
         RaycastHit2D raycastHit;
         float raycastDistance = 0.05f;
         int layerMask = 1 << LayerMask.NameToLayer("Ground");
         Vector3 box_origin = box2d.bounds.center;
         box_origin.y = box2d.bounds.min.y + (box2d.bounds.extents.y / 4f);
         Vector3 box_size = box2d.bounds.size;
         box_size.y = box2d.bounds.size.y / 4f;
         raycastHit = Physics2D.BoxCast(box_origin, box_size, 0f, Vector2.down, raycastDistance, layerMask);
         if (raycastHit.collider != null && !isJumping)
         {
             isGrounded = true;
         }
         else if (raycastHit.collider != null && isJumping)
         {
             isGrounded = true;
             SoundManager.Instance.Play(MegaManLandClip);
             isJumping = false;
         }
     }
 
 
 
 void PlayerMovement()
         if (keyJump && isGrounded)
         {
             isGrounded = false;
             isJumping = true;
             if (isShooting)
             {
                 animator.Play("MegaManShoot+Jump");
             }
             else
             {
                 animator.Play("MegaManJump");
             }
 
             rb2d.velocity = Vector2.up * jumpSpeed;
 
             if (rb2d.velocity.y < 0)
             {
                 rb2d.velocity += Vector2.up * Physics2D.gravity.y * fallMultiplier * Time.deltaTime;
             }
             else if (rb2d.velocity.y > 0 && !Input.GetKey(KeyCode.X))
             {
                 rb2d.velocity += Vector2.up * Physics2D.gravity.y * lowJumpMultiplier * 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
1

Answer by Nistroy · Jul 04, 2021 at 08:20 AM

Hi @Fuzzy_Puppy , instead of using a raycasthit (which you only use to check on you are on the ground), you could directly enter your isGrounded variable. You define it from groundCheckPosition that you will have to reference from the inspector with an empty GameObject which will be the player's child and position at his feet.

 bool isGrounded;
     bool isJumping;
 
     public Transform groundCheck;
     public float groundCheckRadius;
     public LayerMask collisionLayers;
     
 
     private void Update()
     {
         isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, collisionLayers);
 
         if (isGrounded)
         {
             SoundManager.Instance.Play(MegaManLandClip);
             isJumping = false;
         }
     }


 private void OnDrawGizmos()
     {
         if (groundCheck != null)
         {
             Gizmos.DrawWireSphere(groundCheck.position, groundCheckRadius);
         }
     }
Comment
Add comment · Show 4 · 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 rage_co · Jul 04, 2021 at 12:42 PM 1
Share

I would like to suggest doing this......to prevent the audio from playing in everyframe

instead of using if(isGrounded) only you should do

  if (isGrounded && isJumping)
     {
       SoundManager.Instance.Play(MegaManLandClip);
       isJumping = false;
     }


There are obviously better solutions for making a nice trigger with strings or integers....but i won't recommend them unless necessary

avatar image Nistroy rage_co · Jul 06, 2021 at 03:52 PM 1
Share

Ah yes thin! I forgot that the sound is playing in an Update method. So I think the solution is maybe to use a boolean:

 bool isGrounded;
      bool isJumping;
      bool playSong = false;
      public Transform groundCheck;
      public float groundCheckRadius;
      public LayerMask collisionLayers;
      
  
      private void Update()
      {
          isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, collisionLayers);
  
          if (isGrounded && playSong)
          {
              SoundManager.Instance.Play(MegaManLandClip);
              isJumping = false;
              playSong = false;
          }
          if(Input.GetKeyDown(KeyCode.Space))
          {
                isJumping = true;
                playSong = true;
          }
      }
  private void OnDrawGizmos()
      {
          if (groundCheck != null)
          {
              Gizmos.DrawWireSphere(groundCheck.position, groundCheckRadius);
          }
      }
avatar image rage_co Nistroy · Jul 06, 2021 at 03:59 PM 0
Share

well no problem...i do some pretty stupid mistakes daily...it's just natural for us all to overlook something

Show more comments

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

121 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

Related Questions

Can't get Sound working but music is fine 1 Answer

AudioClip calculates length wrong 1 Answer

Sound plays right at the beginning 2 Answers

Can't play multiple sounds at once 2 Answers

Sound effects stuttering when player dies? 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