How can i Prevent Kinematic object not to pass through another rigidbody object - unity3d

I am developing an oculus Home like project where i am facing a problem of object passes through walls and other objects. The scene is described as, Ialt texthave an object that has Box Collider and an Rigidbody (Gravity false , Kinematic true) A wall and a Floor with Box Collider and Rigidbody(Gravity false , Kinematic False) I am moving an object with hands movement. Translate object near and far using thumbstick, the problem is that when i move object far the object pass through the wall and floor. As you can see in the image below the object is passing through in the walls and the floor. Please help me and suggest a better solution. Thanks in advance.

the idea behing Kinematic objects is that interaction with Physics will not alter their transforms, so they will pass through other static or kinematic objects. To alter that you can either not use Kinematic rb's or detect collisions manually and adjust for collision results

I think you have one of your collider's Is Trigger set to true.
If the collider on your object has the Is Trigger property set to true then the collider will pass through other colliders but if it's not a trigger then it'll always collide with other non-trigger colliders. So as long as you have two non-trigger colliders they'll always collide no matter the rigid-body and if you need the Is Trigger property set to true there's a workaround you can use two types of colliders on a single object one a trigger and another a non-trigger preferably a Mesh Collider (so the trigger collider will have space to be triggered).

Related

Character falling through floor unity

I had this working just fine until I decided I wanted to change how I organized the colliders just a few minutes ago to avoid having multiple colliders on the same object.
I have a "Player" object with a boxCollider2d with isTrigger=false and an empty child called "triggers" which itself has 2 children, each with a single boxCollider2d and both with isTrigger=true. These triggers are used for detecting when the player is grounded or next to a wall but are not supposed to affect the physics.
I also have a "floor" object which has a sprite and an empty child called "colliders" which has a single child called "ground" with a single boxCollider2d (isTrigger=false) and the layer of the object set to "Ground".
My understanding is that since my player has a non-trigger collider (and rigidbody) and the floor has a non-trigger collider (at least its child does), that the character shouldn't pass through the floor. Also, it's starting from a height above the ground so it's not an intersection problem.
My best guess is that somehow the child colliders of the player being triggers are turning the main collider into a trigger or that for some reason the floor collider being in a child is screwing with it, but I don't know enough about how these colliders work to really know.
The child trigger colliders should not cause it to mess up like that.
Check Project Settings -> Physics 2D -> Layer Collision Matrix. Are your objects not able to collide?

Does a VR object need a Rigidbody for a collider?

In Unity3D, you need rigidbodies on GameObjects that use colliders, as they use physics. You don't need rigidbodies on static GameObjects because they don't use physics, though you still need to have at least one in the calculation.
My situation is this: I want the game to detect putting your head through a wall with colliders. I'm just curious, as the collider on the head won't use physics, does it still need a rigidbody as it's moving, just not with physics?
My question is: Does a VR object need a rigidbody? (and yes it set it to kinematic)
Colliders → Collision action matrix
=> It depends.
Some of the combinations only cause one of the two objects to be affected by the collision, but the general rule is that physics will not be applied to an object that doesn’t have a Rigidbody component attached.
You said
as the collider on the head won't use physics
But note that in general Collision = Physics.
So yes, at least one of the objects involved in a collision needs to be a (kinematic) Rigidbody.
As you can see in the matrix above e.g. for a collision with a static object the event (like OnCollisionEnter) will be invoked only on the object with a (kinematic o not) Rigidbody but not on the static object.
static here means it has no Rigidbody component attached even if it is moved by code or in other words: As soon as something moves in your scene it should have a (kinematic) Rigidbody component!
You can add colliders to a GameObject without a Rigidbody component to create floors, walls and other motionless elements of a Scene. These are referred to as static colliders. At the opposite, colliders on a GameObject that has a Rigidbody are known as dynamic colliders.

Unity - handle OnCollision on child's Rigidbody

I have a GameObject, constructed from bunch of pills, where each pill is basically constructed from two spheres and a cylinder.
So the GameObject hierarchy looks something like this:
Player (Empty GameObject)
Pill (Empty Game Object)
Sphere (3D sphere)
Cylinder (3D cylinder)
Sphere (3D sphere)
Hopefully the picture will give you guys the visual illustration of what the GameObject looks like:
Now, my goal is to have the entire GameObject - Player as a Rigidbody, while I would like to detect collision on Pill level. Hence I added Rigidbody to Player and Capsule collider to each Pill. However such configuration does not work - the Pill does not receive OnCollisionEnter event. I found suggestion solution, however it does not work for me either - if I set Capsule collider property Is Trigger, then Player does not interact with other Rigid bodies. The only solution I found so far is to add Rigidbody to each Pill, but I am concerned about performance is such scenario.
To sum up - my question is - can I have the above configuration - parent rigid body, child collider work and accept OnCollision events? Of course if I can have parent rigid body and a child with Is Trigger set will also work in case the Player keeps rigid body physics behavior.
I don't know why you want to detect collision on Pill Level but retaining the same configuration one thing you can access is the Collision ContactPoint.

Objects pass through the player object

I have spheres and a player gameObjects, the balls enter the player even though it has a rigid body and a collider, this is how the player is defined:
It has several child objects but none of them has a rigid body or a collider.
This is how the spheres are defined with its sphere child:
The spheres are moved only like this transform.GetComponent<Rigidbody> ().AddForce (dir.normalized * ForceFactor);
So what is causing them to go into the player?
Your player and enemy object both has collider's property IsTrigger enabled. Check out documentation on trigger - it's mentioned that trigger doesn't register a collision. You should use not trigger mode colliders (for both object) to register collision properly and don't allow object to pass through each other

Compound Collider Trigger

I have a parent object, which has four children cubes. The parent has rigidbody, no collider. The four cubes only have colliders, which trigger tags are set true. I want to handle collision in the parent object. However, it didn't work. I know use onCollisionEnter may work, but I must use trigger. Could anybody give me some ideas?
Trigger colliders are unable to use OnColliderEnter because things pass through them instead of colliding. If you wish to detect when an object enters a trigger collider, use OnTriggerEnter instead.
https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html
If however you wish to keep the trigger colliders and make them collide only with specific kind of objects, you can add a second set of colliders that are not triggers and use layers to differentiate which objects can collide with them and which ones pass through. You can then go to Edit->Project Settings->Physics and at the bottom at Layer Collision Matrix configure which layers can collide with each other.
https://docs.unity3d.com/Manual/LayerBasedCollision.html