- Home /
On collision between two cubes
Ok so I have one cube on the ground, it has the trigger on, and there are other cubes with animations coming down from the sky. Pretty much if you don't tap the cubes on time, they fall down and hit the cube with the trigger. My code doesn't work for some reason, I have tried making the cubes falling from the sky the trigger with the code, but still no luck. Any Help? Thanks.
#pragma strict
function OnCollisionEnter (col : Collision)
{
if(col.gameObject.name == "Key1")
{
Application.LoadLevel(1);
}
}
Answer by vyonox · Apr 09, 2016 at 07:03 AM
I think the previous reply is correct but I can add more details.
GameObjects with colliders have two different events for collisions: OnCollisionEnter and OnTriggerEnter. Both need a collider attached to the GameObject, but the first one is only for Colliders with the option "Is Trigger" not enabled. The second function is for Colliders with the option "Is Trigger" enabled.
As you may know, when a Collider with the option "Is Trigger" collides with another Collider, there is no physic reaction. There is no visible collision. The collision can be detected with the function OnTriggerEnter:
void OnTriggerEnter(Collider other) {
Destroy(other.gameObject);
}
This can be used to detect when one GameObject reaches a position or touches another object and you want to collect the object. For example when the player touches a coin and the coin disappears. The function parameter is Collider other, that is basically the GameObject that touched this Collider. This option is a little strange if the GameObject is not destroyed or maybe it is transparent or something similar.
The other option is also perfectly valid. When the Collider has not enabled "Is Trigger", you have the function OnCollisionEnter. The two GameObjects will have a normal collision, with physic reaction, and you will know when this happens and also information of the collision (the parameter is Collision collision). This is useful to play a collision sound or create some special effect.
So, one or another depends of the kind of reaction you want when the two objects collide.
Answer by JigneshKoradiya · Apr 09, 2016 at 05:49 AM
#pragma strict function OnTriggerEnter (col : Collider) { Debug.Log(col.gameObject.name); if(col.gameObject.name == "Key1") { Application.LoadLevel(1); } }
make trigger on of grounded cube and put this script on that dont check the trigger of other cube
copy it again from here now i edited typing mistake
Ok I did what you said, and put the script on the ground cube and made it the trigger, but still nothing is happening.