Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by pitbull3636 · Mar 21, 2016 at 05:44 PM · collisionsphereoncollisionenter

Why the collisions are detected randomly?

Hi everyone,

I am trying to detect the collision between two object. Basically there are 5 spheres that will collide whit a slightly moving object. First the first sphere, than I will put the gravity to the second one and so on. Everytime I destroy the collided sphere. In the last sphere I reset the scene. I print "collision detected (name_of_the_sphere)" everytime that the collision happens. The problem is that sometimes is really random. In particular the collision detected sometimes appear twice for some sphere, sometimes only once, then 2 times..how is it possible?

ps: the object on which the spheres impact is kinematic

 void OnCollisionEnter(Collision col)
     {
         print("Collisione center");
 
         GameObject altodx = GameObject.Find("altodx");
         Rigidbody rb = altodx.GetComponent<Rigidbody>();
         rb.useGravity = true;
 
         //play the audio
         cube.GetComponent<AudioSource>().Play();
        
         GameObject center = GameObject.Find("center");
         Destroy(center);

Comment
Add comment · Show 2
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 Brocccoli · Mar 21, 2016 at 08:53 PM 0
Share

Well, you're never using the actual game object that collides with the rigid body.

You are never using the "col" variable, ins$$anonymous$$d you are getting the "altodx" GameObject which I assume can return any of the spheres named that in any order the engine decides to get them in.

You should be using

 GameObject altodx = col.gameObject;
avatar image pitbull3636 Brocccoli · Mar 22, 2016 at 05:14 PM 0
Share

I tried but doesn't work..it still count random numbers of collision ins$$anonymous$$d of just one

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Abhiroop-Tandon · Mar 23, 2016 at 12:20 PM

you need to check from what you are colliding so usually you either check by a tag or name, something like

 void OnCollisionEnter(Collision col)
          {
              if(col.gameObject.CompareTag("Sphere")){
                  print("Collisione center");
              }
          }

So now it will know that yeah i collided with the Sphere and now i have to print "Collision centre".

Comment
Add comment · Show 5 · 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 pitbull3636 · Mar 23, 2016 at 12:26 PM 0
Share

Thank you. I tried something similar. Basically as you said I check when a sphere impact on a precise object, my kinematic object. But then I still have the same problem, sometimes it print me twice the "collision detected" and sometimes just once. And in this case I also have a delay on the collision of the first sphere (before being destroyed, that is, the sphere impact, then roll for a second on the collider and then it is destroyed)..

I also tried to put a bool variable as false, then as true inside the collision detection function, and a control that if the bool is true then print something. But again, sometimes it print twice and sometime only once. I really can not understand where the problem is!

avatar image Abhiroop-Tandon pitbull3636 · Mar 23, 2016 at 12:31 PM 1
Share

Then under the rigidbody component of your game object change the Collision Detection property to continuous

avatar image pitbull3636 Abhiroop-Tandon · Mar 24, 2016 at 02:29 PM 0
Share

O$$anonymous$$, I put the control you suggested and now it seems to work! YEAH! Thank you very very much! :)

Show more comments
avatar image spooneystone · Mar 23, 2016 at 12:46 PM 1
Share

Yep do what abhiroop said

avatar image
0

Answer by pitbull3636 · Mar 25, 2016 at 02:39 PM

Not really solved...Is still random from time to time.. I don't know what else to try. So I am trying to disable the script after the first execution and re enable it later during the execution of the scripts of the other spheres.

UPDATE: after the collision I disable the detection of the collision like this: GetComponent().detectCollisions = false;

and then I reactivate the collision detection on another sphere. BUT STILL sometime it count 2 collision instead of one! is impossible!! Theoretically after the first one the detection is disabled! how is it possible that it print two times "collision detected"????!!

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

63 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

Related Questions

Make an object fly when it hits another 0 Answers

Check for collision ONCE 0 Answers

Collider seems bigger than it is 0 Answers

OnCollisionEnter Trigger 1 Answer

Why is OnCollisionEnter called in every frame? 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