Detecting a collision between two colliders - unity3d

I'm developing a game in unity using UnityScript. I've initially created two objects, a sphere and a cube, having respective colliders. Now I'm trying to detect a collision between them using the below UnityScript function. But I'm not able to detect collision. I've also added a rigid body component to both of them. How can I detect a collision?
function OnCollisionEnter(col : Collision)
{
if(col.collider.tag=="Cube")
{
Debug.Log("collision");
}

Things to verify:
Make sure the script(s) are attached to the game objects
UPDATED: Make sure the class you have this code in inherits from MonoBehaviour (C#, boo)
Make sure collider components are attached to the game objects and that "Is Trigger" isn't checked as OnCollisionEnter won't be fired if it is.
Make sure the gameobject has the "Cube" tag assigned in the inspector
Also, the rigidbody component only adds physics to the objects and is not necessary when simply detecting collisions.

Related

Prototype 1 Challenge in Unity - adding Rigidbody to game assets

I have added a Rigidbody to each of the obsticles in the Prototype 1 Challenge in Unity. The "player" (plane) game object already has a rigidBody attached. However, when I play the game the plane passes through the obsticles.
How can I make the obsticles solid. I want to use them as triggers to remove points from the player when they collide?
Suggestions:
If you use any collider as a trigger then other objects will pass through it no matter if they are colliders or triggers so just uncheck that property (in your case its box collider) uncheck all of 'is Trigger' then they will become the collider as you want them to be and after that you can have some script which detects collision to check if you collided with obstacle you remove the points from player.
Hope it works... Happy coding :)

Unity 2D On Collision (Do Something) not working?

I know this question has probably been asked many times, but I'm going to ask again.For some reason, my colliders won't work. I have one on my block sprite, and another on my 'miner' sprite. When I hit play, the two start on top of each other. (I'm not sure if this matters, it appears to make no difference.) Each one has a Box Collider 2D. In the script assigned to one of them, it tries to see the collision.
void OnCollisionEnter2D(Collision2D collision)
{
if (col.gameObject.name == "Miner")
{
GameObject.Find("Miner").GetComponent<miner>().block = block;
}
}
However, this script does not seem to detect the Miner sprite colliding with it. I am certain I am deriving from MonoBehavior, so it's not issue there.
Do either of the GameObjects have a RigidBody2D attached to them? At least one GameObject needs to have a RigidBody in any given collision.
Add some debug statements in your code so that you are sure this method is not running. You should read up here about Collisions in unity. Decide if these objects will be moving and set the appropriate collider. As stated above you will typically need a rigidbody.

A collider is better to be static when checking collision?

So... As far as I know it's only good practice to make any GameObject Static if that object is not meant to move, therefore I also thought even a rolling stone with a rigidbody shouldn't be a static GameObject.
However, I was commented by someone as a 'Unity Tip FYI' the physics calculation spikes up when an object checks collision whilst it's rotating, so to make the collider static for the sake of better performance(even meer small amount).
I'm really not getting this idea.
Does this make any sense to anyone here??
Not exactly sure what that comment is saying. It doesn't make sense. Also, it looks like you are confusing static collider and marking GameObject as static.
Marking Object as static from the Editor
Telling Unity to batch it. It is used to increase rendering performance. (This has nothing to do with colliders)
Static Collider
Telling Unity that the Rigidbody will not be moved.
From your question, you are describing Static Collider. Static Collider work for both 3D and 2D physics system.
What is a Static Collider:
Static Collider is when you have a GameObject with any type of collider but no Rigidbody attached to it.
How to create a Static Collider:
To make a GameObject a Static Collider on, attach a collider to that GameObject but don't attach Rigidbody to it. It is now a Static Collider.
To make it non Static Collider, attach Rigidbody to it.
What it is used for:
When you have many Objects that you want your collide with but you are 100 percent sure that this Object will not be moved by you or player.
One good example is a brick wall. You simply want the wall to stop the player but you don't want to move the wall. It could also be used for stones in an environment to prevent player collison. Imagine having to attach hundreds or thousands of Rigidbodies to each stone Object with a collider.
It is used to increase physics performance.
What happens when you move a Static Collider:
Don't. When GameObject is made to be Static Collider, it is expected that you never move it. If you do, it will spike cause spike in CPU. This introduces constant freezing in your game each time you try to move it. This spike has been reduced in Unity 5 but it is still there. I think that's what that comment is trying to say. This whole thing happens because it has to recalculate the physical world all over again. From Unity's website, below is a screenshot of what happens when you try to move a Static Collider Object.
A static game object is not the same as a static rigidbody/collider, they're two completely different concept.
Static game objects are used to save performance time/memory regarding rendering, navigation meshes, etc., you can read about them here: https://docs.unity3d.com/Manual/StaticObjects.html
Static colliders are a totally different beast. A static collider is used to tell Unity that this object will never move under any circumstance, so it will save quite some calculations in the physics engine.
Finally, regarding the tip that you mentioned: I think that probably the author of that tip was talking about 3D colliders, and by "static" he meant that you should separate the 3D mesh from the 3D collider, so when the mesh must be rotated, you rotate only that and not the collider.
Think of a sphere, you create a parent game object, then two children: one that has the Mesh component, and the other the Sphere collider. When you want to rotate the sphere in place, you only rotate the mesh child and not the collider, and when you want to translate instead the sphere, you just translate the parent game object in order to change the position of both children.

Collision Events in Unity3D

I noticed that OnCollisionEnter2D does not trigger when two objects with Collider2D components make contact. It only triggers when one of them has a RigidBody attached to it. This seems odd to me, because the Unity editor itself says that having a static collider in place of a non moving RigidBody is much better for performance. Then why does Unity not allow two game objects with static colliders trigger collision events when they make contact?
Sure, static colliders are more performant, but as the word indicates: they are static.
Movement requires physics calculation and therefore a rigidbody (which holds information about speed etc.). This is actually stated in the docs for the normal collision here http://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html

Unity 2d 5 Collider not working

Using Unity 5.0.1f1
I'm trying to make it so that when I shoot, if it hits an invisible object it destroys it, but when it collides nothing happens. Here is the code:
void OnTriggerEnter2D(Collider2D col){
Destroy (col.gameObject);
Debug.Log ("find");
}
It's hard for me to give you an answer given the little information that you are providing. But, the most possible solutions to your problem are:
Attaching a Rigidbody component to one of the colliding objects.
Making the object which will be destroyed have a normal collider attached and the other object must have a trigger collider attached.
Make sure that all of the Colliders/Triggers and Rigidbodies you use are 2D. That is, Rigidbody2D and Collider2D. Because you are using the void OnTriggerEnter2D method.
Make sure that the colliding objects are in layers which collide with each other. You can check which layer collides with each layer by going to Edit-->Project Settings-->Physics2D (Or Physics if you end up using 3D Physics).
Hope this helps!