OnCollisionEnter not called on Unity4.3 for 2D - unity3d

I have two 2D object, one is use to throw by catapult and then it hit other 2D object. Both have collider and Rigidbody both have unchecked isKinetic and isTrigger. When they collide with each other then OnCollisionEnter method doesn't call and script is attached to that object which is thrown by catapult.

Try using OnCollisionEnter2D(Collision2D collision)
I also had that problem and this is what fixed it for me. Good luck to you.

Related

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

Unity how to find if object intersects with mesh collider (not bounding box, the actual mesh)?

i know how to detect basic bounding box collisions
Collider collider1= obj1.GetComponent<Collider>();
Collider collider2= obj2.GetComponent<Collider>();
if (collider1.bounds.Intersects(collider2.bounds))
Debug.Log("collides with=" + objCollider.name);
but that's not very accurate, i'd like to get if their actual meshes collide so i tried
MeshCollider collider1= obj1.GetComponent<MeshCollider>();
MeshCollider collider2= obj2.GetComponent<MeshCollider>();
if (collider1.bounds.Intersects(collider2.bounds))
Debug.Log("collides with=" + objCollider.name);
and no difference, any suggestions please?
thanks
UPDATE, please note i forgot to add that i need this to work when timescale is zero and collisions don't work then
That's not the correct way to detect collisions. Use MonoBehaviour.OnCollisionEnter or MonoBehaviour.OnTriggerEnter instead.
OnTriggerEnter is called when the Collider other enters the trigger.
OnCollisionEnter is called when this collider/rigidbody has begun touching another rigidbody/collider.
Just attach the following script to the GameObject that will collide with another GameObject. The method will be called when the collision happens.
using UnityEngine;
public class ExampleClass : MonoBehaviour
{
void OnCollisionEnter(Collision collision) // or void OnTriggerEnter(Collider other)
{
//...
}
}

Unity collision not being detected

I am learning to create a simple fps game in unity the problem is that the collision does not update itself for example initially when my player is on the ground console prints "floor" by "Debug.log(collision.gameObject)" but when it intersects other objects such as a cube console will print out "cube" but when I walk away from it , console does not change back to "floor" Why????
I am using transform.translate to move and jump and using method OnCollisionEnter for collision detection
OnCollisionEnter is triggered only when object enters the collider.
A) Make a list of all encountered objects by adding them when OnCollisionEnter happens and removing when OnCollisionExit happens. Then whenever you need to make sure you are on "floor" check it in the list.
B) Use OnCollisionStay and every frame you will be notified if you are touching the "floor".
Remember one thing, the other object you want to collide with need to have a collider component asigned, make sure of it. Join this with the previous answer.
I recommend to verify collision.
Here on simple example:
void OnCollisionEnter (Collision col){
if (col.gameObject){
Debug.Log("Object name : "+ col.gameObject.name);
}
}

How to detect two object collide or not?

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
}

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.