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 /
avatar image
0
Question by rish209 · Aug 02, 2016 at 01:13 AM · collisioncollidercollision detectioncubeoncollisionenter

Weird Collision Detection with a Cube and OnCollisionEnter

In my project, I use OnCollisionEnter to detect when a cube hits another cube. For some reason, the cubes detect each other without actually colliding. My program has the cubes fall when they collide and each time they start to fall, they are a small distance away from each other.

Careful testing reveals that the area in which the collisions are detected are in what seems to be a sphere with diameter sqrt 3 (cubes have side length 1). Is there any way to fix this or figure out why it is happening?

Edit (Additional Info and Code):

I checked and my cube definitely has a box collider and no other colliders. It is also not the child of an object. It has spheres with no rigidbodies and colliders set to triggers as children but deleting them had no visual effect on the problem.

Here is the OnCollisionEnter which sets a variable that is used in another script.

Note: There is some code related to SteamVR since I am building this game as a virtual reality experience.

  void OnCollisionEnter(Collision other)
     {
         if (other.collider.gameObject.CompareTag("Connecter") == false)
         {
             if (other.collider.gameObject.name != "Ground")
             {
                 if (other.collider.gameObject.GetComponent<Held>().isCarried() == true && this.isCarried() == true)
                 {
                     //yetAnotherBool = true;
                     otherP = other;
 
                     var deviceL = SteamVR_Controller.Input(indexL);
                     var deviceR = SteamVR_Controller.Input(indexR);
 
                     var rb = this.gameObject.GetComponent<Rigidbody>();
                     var otherRB = other.collider.gameObject.GetComponent<Rigidbody>();
 
                     if (rb.velocity.magnitude > otherRB.velocity.magnitude)
                     {
                         if (Mathf.Abs(rb.velocity.magnitude - deviceL.velocity.magnitude) > Mathf.Abs(rb.velocity.magnitude - deviceR.velocity.magnitude))
                         {
                             MotherScript2.booleanR = true;
                         }
                         else
                         {
                             MotherScript2.booleanL = true;
                         }
                     }
                 }
             }
         }
     }

Here is a place where the function ReleaseCube() is called:

 else if (PickUpJoint != null || boolean == true)
             {
                 if (BlockP.gameObject.GetComponent<Held>().isHeld() == true)
                 {
                     if (trackedController.triggerPressed == false || boolean == true)
                     {
 
                         ReleaseCube(controller);
                     }
                 }
             } //Remove Fixed Joint and Release Cube

Here is the ReleaseCube function:

 public void ReleaseCube(Transform controller)
     {
         index = (int)trackedController.controllerIndex;
         var device = SteamVR_Controller.Input(index);
 
         Debug.Log("Test");
 
         GameObject go = PickUpJoint.gameObject;
         Rigidbody rigidbody = go.GetComponent<Rigidbody>();
         Object.Destroy(PickUpJoint);
         PickUpJoint = null;
 
         SetIsHeld(false, "OnTriggerStay in " + controller.gameObject.name);
         BlockP.gameObject.GetComponent<Held>().isCarried(false);
         this.SetIsHolding(false, "OnTriggerStay in " + this.gameObject.name);
         BlockCounter--;
 
         var origin = trackedObj.origin ? trackedObj.origin : trackedObj.transform.parent;
         if (origin != null)
         {
             rigidbody.velocity = origin.TransformVector(device.velocity);
             rigidbody.angularVelocity = origin.TransformVector(device.angularVelocity);
         }
         else
         {
             rigidbody.velocity = device.velocity;
             rigidbody.angularVelocity = device.angularVelocity;
         }
 
         rigidbody.maxAngularVelocity = rigidbody.angularVelocity.magnitude;
     }

Sorry for the confusing code. Here is what it does/is supposed to do:

The first part detects collisions between two carried cubes and finds which one is moving faster and what controller it is held by. It then sets a boolean true (the boolean is set false constantly in an update loop).

The second part checks to see if the cube is being held and the trigger isn't pressed or if the boolean is true. It then calls the ReleaseCube function.

The third part destroys the joint between the controller and the cube, sets a bunch of variables that say the cube is held to false, and sets the cube's velocity to equal that of the controller.

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 Trevdevs · Aug 02, 2016 at 02:52 AM 0
Share

If you could post the code that would be most helpful :) What I'm thinking is your just confusing the collider with a rigidbody or navmeshagent, or the possibility your collider isn't a box collider.

avatar image rish209 · Aug 02, 2016 at 03:15 AM 0
Share

It wouldn't let me comment so I edited the original post to include more info.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by gouthammannuru · Aug 02, 2016 at 01:38 PM

Are these cubes Prefabs in that case delete the prefab and make a new prefab i had the same kind of problem and doing this (Deleting the prefab's) i got things to normal (I haven't checked your code )as i faced the problem previously and this is what i applied to get things right

Comment
Add comment · Show 1 · 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 rish209 · Aug 02, 2016 at 05:12 PM 0
Share

Thanks for the help. I completely made the new cube prefab from scratch by creating new gameobjects and modifying their properties. Unfortunately, trying this does not seem to have made a difference.

avatar image
0

Answer by rish209 · Aug 02, 2016 at 05:45 PM

While this isn't a real solution, as a temporary fix until I figure out the problem, I am setting this BoxCollider's size to new Vector3(0.8f, 0.8f, 0.8f) while the variable isCarried == true (one of the conditions in the OnCollisionEnter).

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

61 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

Related Questions

Colliding two GameObjects 1 Answer

How do i understand which gameObject is player touching ? 1 Answer

Avoid Player bouncing when colliding with objects 3D 1 Answer

Cube with colliders ignores other colliders, why? 1 Answer

Colliders not working when rotating 2 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