- Home /
I need to detect cube collision with the plane, when it is falling down
So, basically, when the cube will land onto the ground I need to detect this place in order to deform the surface, i.e make a hole in it. I would be very grateful if the script will be provided in the answers as long as some nice ideas :)
Answer by BastianUrbach · Apr 08, 2018 at 09:00 PM
When a collision occurs, the method OnCollisionEnter is called on all scripts on the objects that are involved. The Collision object that's passed as parameter contains all relevant information about the event. In your case that seems to be the array of contact points (Collision.contacts). So all you need to do is put something like this in your script:
void OnCollisionEnter(Collision collision) { foreach (ContactPoint contact in collision.contacts) { // contact.point is a world space position where the objects collided doSomething(contact.point); } }
Answer by tormentoarmagedoom · Apr 08, 2018 at 09:01 PM
Good day.
You need to use the OnTriggerEnter() method.
The plane and the cube must have colliders. Then the cube collider must be marked as trigger by inpector.
Then you need a script like this in the plane:
void OnTriggerEnter(Collider Cube)
{
// The code you need to be executed. 'Cube' variable is the collider of the cube, so Cube.gameObject is the cube.
}
You should read the APIand some tutorials about collisions/triggers.
Accept the answer if helpped :D
For this case OnCollisionEnter would likely be the better choice as it detects the contact points of the collision, something OnTriggerEnter does not do. This could be worked around with code that deter$$anonymous$$es where the cube is relative to the ground, but OnCollisionEnter will give the required information while allowing the physics to do its thing and not have the cube pass through the ground.