- Home /
Using OnCollisionEnter with cube
Hi I am using OnCollisionEnter with a cube, but anytime the player(Which is just a capsule for now) touches the cube nothing happens. I am looking the cube to be destroyed whenever the player touches it. Both the player and the cube have ridge bodies, both of them do not have is Kinematic checked. Any help would be great :)
void OnCollisionStart (Collision col)
{
if (col.gameObject.name == "PlatformBroken")
{
Destroy(col.gameObject);
}
}
Answer by NightmarexGR · Jan 04, 2014 at 11:23 PM
Instead of:
void OnCollisionStart (Collision col)
Use:
void OnCollisionEnter(Collision col)
Hope it works.
~NightmarexGR
Answer by AlucardJay · Jan 05, 2014 at 12:08 AM
Collider : http://docs.unity3d.com/Documentation/ScriptReference/Collider.html
OnCollisionEnter OnCollisionEnter is called when this collider/rigidbody has begun touching another rigidbody/collider.
OnCollisionExit OnCollisionExit is called when this collider/rigidbody has stopped touching another rigidbody/collider.
OnCollisionStay OnCollisionStay is called once per frame for every collider/rigidbody that is touching rigidbody/collider.
OnTriggerEnter OnTriggerEnter is called when the Collider other enters the trigger.
OnTriggerExit OnTriggerExit is called when the Collider other has stopped touching the trigger.
OnTriggerStay OnTriggerStay is called almost all the frames for every Collider other that is touching the trigger.
OnCollisionEnter : http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnCollisionEnter.html
Description : OnCollisionEnter is called when this collider/rigidbody has begun touching another rigidbody/collider.
Note that collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached.
^^ is this true for your objects?
Yeah my player has a rigidbody attached, and so does the square. Does it matter if I have use gravity not checked?
Add a debug in your collision event to find out if the collision is taking place, and the names of the gameObjects involved :
void OnCollisionEnter (Collision col)
{
Debug.Log( gameObject.name + " just collided with " + col.gameObject.name );
if (col.gameObject.name == "PlatformBroken")
{
Destroy(col.gameObject);
}
}
also in future, please format your code. You can do this by highlighting all your code, then clicking the 10101 button at the top of the edit window.
I figure it out, it was to do with the mesh size of my character it wasn't actually touching the square! Thanks for the help everyone :)
Your answer
