I’m making a little snowboarding game and I’m making a rail. I added a physics material to the character that makes him only able to go in one direction. I want to change the physics material when he comes into contact with a rail. I put the rail on a different layer, I just don’t know how to tell if the character collided with the rail or not.
Let’s assume you’re using OnCollisionEnter()
to catch the collision. By using the Collision
argument that’s passed in, you can read some information about the other object – for example, its layer:
function OnCollisionEnter(collision : Collision)
{
Debug.Log(collision.collider.gameObject.layer);
}
You probably want to check if that object is on a particular layer, though:
function OnCollisionEnter(collision : Collision)
{
if (collision.collider.gameObject.layer == LayerMask.NameToLayer("LAYER_NAME"))
{
Debug.Log("Touched a rail");
}
}
Some script reference pages that might be helpful:
- http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.OnCollisionEnter.html
- http://unity3d.com/support/documentation/ScriptReference/Collision.html
- http://unity3d.com/support/documentation/ScriptReference/Collider.html
- http://unity3d.com/support/documentation/ScriptReference/LayerMask.html