• 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 /
  • Help Room /
avatar image
0
Question by Statmuffin · Jun 03, 2020 at 06:50 AM · collisionaudiobug

Floating Objects Despite Gravity Used and No Colliders Preventing Fall,Floating Object in Unity Despite Collision Underneath Removed

At the moment I'm creating a breakout style game where the blocks are removed when hit by a ball, the blocks stack on top of each other and use Unity's built in gravity with constraints on everything but the y-position to function. All was fine with the game as I was destroying the objects as they were hit, however once I wanted to add audio to the collision this was no longer viable as the object had to be around long enough to play the sound, but disappear on contact, this was my solution. alt text

For the most part this is functional code, most blocks fall as you'd expect though every time I run the game, 2-4 blocks remain in the air, even once the object underneath has been destroyed as can be seen here: alt text

There is definitely no gameobject underneath them, no collision box that isn't the table which uses an exact box collider, as does the block itself. Editing the rigidbody or collider in any way causes it to update in some way and realise there's no collision underneath it, proceeding to fall as expected. From changing the detection mode to turning gravity on and off again it seems as if the object just forgets its supposed to check if there's something underneath it to fall.

If anyone has any ideas or alternate solutions I'll take anything at this point, thanks in advance!

codesnippet.png (13.8 kB)
floatingblocks.png (189.3 kB)
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 SteenPetersen · Jun 03, 2020 at 06:51 AM

I believe this is an architecture problem you are seeing here, and you may wish to read about the Single-responsibility principle.

I would rather make a SoundManager that is always alive and has the responsibility to play sounds, that way you can kill, destroy or pool your objects.

Something like this:

 using UnityEngine;
 
 [RequireComponent(typeof(AudioSource))]
 public class SoundManager : MonoBehaviour
 {
     public static SoundManager instance;
 
     [HideInInspector]
     public AudioClip exampleSound, secondExampleSound; // stored in Resources/Sounds
 
     AudioSource _audioSource;
 
     private void Awake()
     {
         if (instance != null && instance != this)
         {
             Destroy(this.gameObject);
             return;
         }
         else
         {
             instance = this;
         }
 
         _audioSource = GetComponent<AudioSource>();
     }
 
     void Start()
     {
         exampleSound = Resources.Load<AudioClip>("Sounds/" + "exampleName");
         secondExampleSound = Resources.Load<AudioClip>("Sounds/" + "secondExampleName");
     }
 
     /// <summary>
     /// Plays the sound with a matching name to the first parameter from the
     /// audioSource passed in as the second parameter
     /// </summary>
     /// <param name="clip">Name of the clip to be played (camelCase format)</param>
     /// <param name="src">Audiosource to play this sound from</param>
     public void PlaySound(string clip, AudioSource src)
     {
         switch (clip)
         {
             case "exampleName":
                 src.PlayOneShot(exampleSound);
                 break;
 
             case "secondExampleName":
                 src.PlayOneShot(secondExampleSound);
                 break;
         }
     }
 
     public void Play(string s)
     {
         if (!_audioSource.isPlaying)
         {
             PlaySound(s, _audioSource);
         }
     }
 }
 

  


Then you can simply call it like so:

 // play from specific audio source
 SoundManager.instance.PlaySound("exampleSound", myAudioSource);
 
 // or
 
 // play from general audio source
 SoundManager.instance.Play("exampleSound");


I hope this helps.

P.s, remember to not post code in screenshots but rather press the 101/010 button and paste your code, that way it is easier for people trying to help you to debug your code.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

309 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Audio not playing in Play Mode or build 1 Answer

Multiple colliders BUG 0 Answers

Raycasting Stopped Working all of a sudden 0 Answers

collision glitch 0 Answers

Sound in Unity is not working! WHY? 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