- Home /
Platform collision detection
I want a platform that will destroy itself if the player is moving beyond a certain speed. However, I can't find the collision function that will work for me. I can't do OnTriggerEnter because if I check the 'Is Trigger' on the platform's box colider, the player will just fall through. The player has a Character Controller attached, but I'm finding that OnControllerColliderHit won't work either. Any suggestions?
Answer by ScroodgeM · Aug 07, 2012 at 07:55 PM
AFAIK OnControllerColliderHit method works only on character collisions itself. so you should use OnCollisionEnter on platforms to detect player...
Answer by Muuskii · Aug 07, 2012 at 07:59 PM
Note how in OnCollisionEnter you actually have a relative velocity value that you can check. If it's higher than a threshold Destroy the platform and make sure to set the Rigidbody.velocity like so:
Other.rigidbody.velocity = rigidbody.relativevelocity * scaler; //He loses a little bit of speed
Edit: Oops, just saw: Character controller (hate those) not rigidbody. Oh well, my suggestion stands.
I managed to get it working by adding another game object parented to the platform that isnt rendered and has isTrigger checked, and then i just attach the script to that.
Answer by Develogamer · Aug 07, 2012 at 08:02 PM
If you want to detect when the player hits a platform then you can use OnCollisionEnter(), which is called the first frame of a collision. You can put a collision parameter on the function to get more info on the actual collision i.e the force on the collision and such. For example, in c#:
void OnCollisionEnter(Collision collision)
{
if(collision.gameObject == platform)
Destroy(collision.gameObject);
}
If the player is already on the platform you can use OnCollisionStay which is called every frame that the player is colliding with it. Hope this helps.
Here is the link with the info you can get from the Collider class: http://docs.unity3d.com/Documentation/ScriptReference/Collision.html