Unity3d: OnCollisionEnter only work if both collider's isTrigger=false - unity3d

So the bullets have an OnCollisionEnter setup but will not trigger unless the character and the bullet are isTrigger=false
I wouldn't mind the character having isTrigger = false but the bullests need to be isTrigger=true. Otherwise the bullets impact moves the character which is undesirable. Both items have Rigidbodies.

The OnCollisionEnter callback is used when none of the colliders has isTrigger enabled or set to true.
If any of the colliders you want to detect touching each other has isTrigger enabled then OnTriggerEnter should be used instead of OnCollisionEnter. In your case, use OnTriggerEnter since one of your colliders has isTrigger enabled.
To learn more about this, read the collision matrix table from the doc.

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.

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

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).

collision not happening for same object in unity

objects are not fast moving, they are very slow 6 same objects sometimes collision works sometimes not for same objects? Is it usual?
I am using ontriggerenter everything is working but sometimes some object on hexagon not collapsing.
1 ball hitting 6 same pieces. 5 of them collapsing 1 is not collapsing every time, and its changing
their tag is same name
OnTriggerEnter would not allow a physics interaction from two objects. The GameObject with the Collider set to isTrigger = true would pass through the other object. From MonoBehaviour.OnTriggerEnter(Collider):
Description
OnTriggerEnter is called when the GameObject collides with another GameObject.
The given other Collider has details about the trigger event, such as the name of its GameObject. Either of the two GameObjects must have a Rigidbody component. The Rigidbody component has both Rigidbody.useGravity and Rigidbody.isKinematic set to false. These prevents the GameObject from falling under gravity and having kinematic behavior. One Collider has Collider.isTrigger set to true. The GameObject with Collider.isTrigger set to true has OnTriggerEnter called when the other GameObject touches or passes through it. OnTriggerEnter occurs after FixedUpdate ends.

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