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
1
Question by Brokenarrow · Jun 12, 2013 at 09:13 AM · collisionrigidbodyjointkinematicseparate

Rigid Body collision problem

Hi, I've submitted this question before, and on request of a user I added a picture. I also defined the problem more sharply after some tests with the help of some tips this user gave me.

I have a player controlled object, object A. It consists of 2 parts, A1 and A2, which should be locked to each others position. Object B is identical, and can be player controlled or AI controlled.

The goal is as follows:

All parts should be able to collide with each other, and react to physics, as long as the positional lock between two parts(A1 & A2 and B1 & B2, respectively) is kept intact.

All parts should have different colliders. If A1 collides, A2 should NOT receive a collision message.

For some reason, I cant get this to work. I've tried joints, kinematics and all sorts of parent child setups, but I can't seem to get the aimed results. Kinematic parts won't collide with each other and separate colliders require separate rigid bodies, which are hard to keep together (as they react to physics individually). Kinematic rigidbody triggers do work, at least code-wise but do not adhere to physics. :(

Alternatively, a workaround could be if I could make a single collider and detect which side was hit. Is such a thing possible?

Thanks in advance!

bumpercars

unity problem.jpg (25.1 kB)
Comment
Add comment · Show 1
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 Fattie · Jun 12, 2013 at 09:21 AM 0
Share

this is great, UI'm now going to get TWO tick symbols :)

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Fattie · Jun 12, 2013 at 09:21 AM

It couldn't be easier champ:

Here's the full working code. put this on the WRAPPER object, the OVERALL object:

 function OnCollisionEnter(cc:Collision)
     {
     Debug.Log("I got hit ... ny name is ... " + gameObject.name);
     Debug.Log("but even better, here's the specific collider on this side ... " +
            cc.contacts[0].thisCollider.name );
     }

alt text

Look at the image. NOtice the game object called "GameObject" is the WRAPPER object, that's the OVERALL object.

This is completely normal. Almost every single object ever done in Unity would look like this. You have the rigidbody on the WRAPPER object and the colliders on objects BELOW THAT.

Then on the WRAPPER object IS THE ABOVE SCRIPT.

Note - in the example say you particularly want the sphere or the cube to "get" the message that they have been hit. ie for some reason you would particularly rather the code for "cube hit" was actually on the cube. What you do is send that message down to the cube. So you would have a script on the cube called cubeControl. You would modify the sample code above lije this ...

   if cc.contacts[0].thisCollider.name == the cube, then
      cubeControl.youGotHitDude!!!();
 
   if cc.contacts[0].thisCollider.name == the sohere, then
      sphereControl.youGotHitDude!!!();

you understand? Again - this is utterly SOP. Virtually every object ever done in Unity would have this! (Other than the most trivial test projects.)

Don't forget here is the exact forumla:

remember only ONE object has the RIGIDBODY -- the HIGHEST LEVEL object.

put EACH OLLIDER on SEPARATE holder objects UNDER that object.

put my EXAMPLE script on the HIGHEST object (the one WITH the RIGIDBODY)

Finally don't forget as I explained at great length:

When your objects fly apart the way you make video games is they change to different objects

This is completely normal, you have to do the work and you can't "magically" avoid that.

For exampleL in my image say the sphere broke-away. Note that just to begin with the sphere would now need a rigidbody!! It is now a HUGELY DIFFERENT SORT of object!!

It may now be part of a pool in your game, it may be a ragdoll, it may have to float, bounce (who knows), it may be explosive, it may have unusual sound effects ... who knows?

It's a totally different object, this is not magic -- you must program that

As I explained at length, very often you actually secretly swap to another object even though the player will think it's the "same" objects as before.

i hope it helps!

Again the total answer to what you are asking is exactly the same code at the top here.


collide.png (90.1 kB)
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 Brokenarrow · Jun 12, 2013 at 11:04 AM 0
Share

Yep, collision.contacts brought me on the right track and the hierarchy explanation was just what I needed, thanks so much, you're a lifesaver :)

avatar image
1

Answer by shaderop · Jun 12, 2013 at 09:41 AM

I can offer a suggestion regarding this:

Alternatively, a workaround could be if I could make a single collider and detect which side was hit. Is such a thing possible?

I think it's definitely possible if you use the Collision.contacts property inside your collision events. Easiest thing would be to take the first contact point and use the normal to decide on which side the collision happened.

To elaborate, assuming that you have a root object A that is the parent of both A1 and A, with the A1 to the right and A2 to the left of the center, so that the local coordinates are like so:

           y
           ^
           |
           +--> x
 +--------+ +----+
 |   A1   | | A2 |
 +--------| +----+

Then take the normal of the collision contact point and translate it into A's local coordinate space using Transform.InverseTransformPoint. This new normal can be used to find on which side the collision happened using the dot product:

 if (Vector3.Dot(n, Vector3.right) > 0))
   Debug.Log("Collision happened on the right side of A, i.e. A2");
 else
   Debug.Log("Collision happened on the left side of A, i.e. A1");

Of course this isn't 100% percent accurate, but it might be a good enough. There's probably a more elegant solution to this issue, but I must confess I'm not fully grokking your requirements.

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 Fattie · Jun 12, 2013 at 09:51 AM 0
Share

you know, it's clever to use the normal, but for "which region was a collision in" i strongly recommend, simply make two boxes under the object and check which one the point is inside. it's inevitably much more game-like to do this. if you look at say any typical vehicle . it is covered with boxes "the collision happened in side here" (ie, it hit a bumper) or "the collision happened inside here" (ie, something hit the roof .. whatever).

But just as you imply, shader, the question's a bit muddy because (I suggest) broken kind of has the wrong fundamental setup for what he or she is after.

Fortunately, i have explained everything eh!!! :)

avatar image shaderop · Jun 12, 2013 at 09:55 AM 0
Share

$$anonymous$$an, don't rub it in. I'm already awestruck by the wicked awesomeness of your answer. Don't beat a fella when he's down and knows it :)

avatar image Fattie · Jun 12, 2013 at 10:26 AM 0
Share

heh not so awesome :) i guess, it's a basic of using colliders, etc, in unity

it's one of those things "not in the manual".

i mean in the manual they suggest, you have a "car" or whatever and you "add a collider"

but you just never, ever, ever, ever do that in the real world. you have a wrapper object (the car) which is blank and has the rigidbody. below that you have various holder objects with all the colliders.

Really, unity should have set it up so that colliders $$anonymous$$UST BE on separate holder objects below the rigidbody. it's inconceivable, in the real world, you'd have them on the same object

so - it's no big deal.

avatar image shaderop · Jun 12, 2013 at 10:36 AM 0
Share

so - it's no big deal.

I didn't know that, so it's a big honking deal to me. Now things are starting to make a whole lot of sense. Thanks for sharing that nugget of wisdom, Fattie.

avatar image Fattie · Jun 12, 2013 at 12:01 PM 0
Share

not at all, I knew nothing about Unity a few weeks ago, game engines are very confusing if you have previously done normal program$$anonymous$$g on normal computers. i should preface all my comments with "Bunny told me the other day that . . ." :)

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

16 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

Related Questions

Object gravity but no collisions 0 Answers

Rigidbody: Freeze All vs InKinematic 0 Answers

isKinematic object doesnt collide with wall 1 Answer

hing joint issue,Hing Joint Issue in tow truck 0 Answers

rigidbody not moving when switching from kinematic to non kinematic on same collision 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