How to detect two object collide or not? - triggers

I am making one unity game in which two objects having collider in which I have select isTriger and does not have rigid body, if I put the rigid body then they are kinematic object, So that gravity did not effect on that object, Even also i don't want any physic operation on this object also. but I want to detect whether this two object collide which each other or not.
How can I do this ?

When 2 colliders make contact with each other,
OnCollisionEnter2D
OnCollisionExit2D
OnCollisionStay2D
are called for 2D games, likewise for 3D(remove 2D in names) also.
Check out this link: http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnCollisionEnter2D.html

Sorry man, but unity use the phsysics engine to detect collision so you have to add the rigidbody to the item you want to to plug the script.
PS:remeber if you want to detect collisions with Trigger collider, you need to use
void OnTriggerEnter(){
//your code
}
void OnTriggerStay(){
//your code
}
void OnTriggerLeave(){
//your code
}

Related

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

Unity collison not detected 2d

I am building a 2d game and I run this code with a collider on both the other objects (on trigger: on to the other object) but nothing works)The other object is name Coin and also it has the tag Coin.
I added kinematic rigid body to the coin and still no work :( (If I deleted it still the same). Also, my player has to colliders and the collision detected on both coin and player are discrete. Also, they are prefabs both
void OnTriggerEnter2d(Collider2D other) {
Debug.Log("detected");
if (other.gameObject.CompareTag("Coin")) {
other.gameObject.SetActive(false);
}
}
Typo mistake: instead OnTriggerEnter2d try OnTriggerEnter2D. with a capital D. ;)
In order to get a OnTrigger collision triggered, you must have the following requirements:
1-The 2 objects need to have a collider attached it.
2-Only one of the objects needs to be a trigger
3-One of the 2 objects must have a rigidbody attached to it.
Did you fulfill the requirement n°3 ?
Typo mistake: instead OnTriggerEnter2d try OnTriggerEnter2D. with a capital D. ;)

Two collider in one game object how to distinguish between them when colliding with another?

I'm making a 2D game, there is one player with two collider : a box collider 2D on top and a small circle collider 2D bottom. When the player jump on the box he will be ok, but when he collide with the box with his face(box collider 2D) he will die ? How can I do that? I tried the following code , but it not work. Please help me.Thanks!
if (GetComponent<Collider2D>().GetType() == typeof(BoxCollider2D))
{
//do something
}
if (GetComponent<Collider2D>().GetType() == typeof(CircleCollider2D))
{
//do something
}
If you want to get a certain BoxCollider, you can use GetComponent<BoxCollider2D>() and GetComponent<CircleCollider2D>()
However, I am not sure how to take this information and check which one fired the the OnCollisionEnter, unless you set one collider to a trigger, and use OnTriggerEnter for one, and OnCollisionEnter for the other.
What I would recommend is having two child game objects on the player and put both colliders on each game object, and handle each event in their own OnCollisonEnter

Preventing colliders on the same rig from colliding with each other. But allowing them to collide with other rigs.

I have a prefab NPC which has a physics rig attached to it (to do some specific rag-doll stuff with). I need to avoid the various colliders on the same rig (arms, legs etc) from colliding with each other, but they have to be able to collide with the rigs of other instantiated NPCs.
Is there a way to do this? I know I can avoid all the colliders from colliding by putting them on a separate layer, but I cant create a new layer for every NPC.
Thanks
You can do by setting up IgnoreCollision on your NPC class if you have any
http://docs.unity3d.com/ScriptReference/Physics.IgnoreCollision.html
So simple loop through all colliders in the rig and set up to ignore each other
void Start() {
colliders = GetComponentsInChildren<Collider>();
foreach(Collider collider in colliders) {
otherColliders = GetComponentsInChildren<Collider>();
foreach(Collider otherColider in otherColliders) {
if (collider != otherColider) {
Physics.IgnoreCollision(collider, otherColider);
}
}
}
}
It looks like the only way to ignore collisions without using layers is to use Physics.IgnoreCollision() between a pair of colliders, for each pair.
You can write some code that would automatically register a newly instantiated game object and create these pairs between the new object and the other ones that were registered before, so you wouldn't need to call this method for each pair yourself.
Or, you can use this code that does that for you :) It has its own representation of layer to control how the objects should ignore each other.

Getting collisions for multiple object without the collision callbacks

I'm making a platformer (sidescroller), and right now I'm making a grenade-launcher for the player.
It spawns grenades which is a prefab, this prefab has a Circle Collider 2D which is the blast radius for the explosion.
When a grenade is spawned I run this (amongst other things);
// Add the initial force
rBody.AddForce(new Vector2(forceX, forceY) *300f);
Invoke("Explode", 2.5f);
I'm having trouble figuring out how I should handle the Explode function.
I would like to find all the GameObjects that are colliding with the Circle Collider 2D at that point, but I can't find a way to do it.
I would like to be able to do something like this (not real code but you understand what I'm trying to do)
void Explode () {
collidingObjects = circleCollider.getCollisions();
foreach(collidingObject as entity) {
if(entity = 'player')
player.pushLeftOrRight()
elseif(entity = 'enemy')
grenade.dealDamage(grenade.damage)
}
Debug.Log("Explode");
Destroy(gameObject);
}
I'm guessing that it wouldn't work so can anyone point me in the right direction?
In your grenade's script, use OnTriggerEnter or OnCollisionEnter to log collisions; basically just have a HashSet for each grenade that is updated and keeps track of what it is colliding with. Then Explode just has to iterate through this set when you call it.