Collision Extensions
Has Tag
This calls the HasTag extension method on the Collision object to determine if the object it collided with has the tag "Enemy". If true, it logs a message indicating the collision.
void OnCollisionEnter(Collision collision)
{
if (collision.HasTag("Enemy"))
{
Debug.Log("Collided with an enemy!");
}
}
Has Layer
This calls the HasLayer extension method to check if the object collided with is in the "Player" layer. If true, it logs a message indicating the collision.
void OnCollisionEnter(Collision collision)
{
if (collision.HasLayer("Player"))
{
Debug.Log("Collided with the player!");
}
}
Get All Colliders
This calls the GetAllColliders extension method on the Collision object, retrieving a list of all colliding objects. It then logs the names of those objects.
void OnCollisionEnter(Collision collision)
{
List<GameObject> allColliders = collision.GetAllColliders();
foreach (GameObject collider in allColliders)
{
Debug.Log("Collided with: " + collider.name);
}
}
Get Colliders With Tag
This calls the GetCollidersWithTag extension method on the Collision object, retrieving a list of all colliding objects with the tag "Enemy". It then logs the names of those enemies.
void OnCollisionEnter(Collision collision)
{
List<GameObject> enemyColliders = collision.GetCollidersWithTag("Enemy");
foreach (GameObject enemy in enemyColliders)
{
Debug.Log("Collided with enemy: " + enemy.name);
}
}
Get Colliders With Layer
This calls the GetCollidersWithTag extension method on the Collision object, retrieving a list of all colliding objects with the tag "Enemy". It then logs the names of those enemies.
void OnCollisionEnter(Collision collision)
{
List<GameObject> playerColliders = collision.GetCollidersWithLayer("Player");
foreach (GameObject player in playerColliders)
{
Debug.Log("Collided with player: " + player.name);
}
}
Get Contact Points
This calls the GetContactPoints extension method to retrieve the contact points of the collision, logging each point's coordinates.
void OnCollisionEnter(Collision collision)
{
Vector3[] contactPoints = collision.GetContactPoints();
foreach (Vector3 point in contactPoints)
{
Debug.Log("Contact Point: " + point);
}
}
Get Contact Points
This calls the GetContactPoints extension method to retrieve the contact points of the collision, logging each point's coordinates.
void OnCollisionEnter(Collision collision)
{
Vector3[] contactPoints = collision.GetContactPoints();
foreach (Vector3 point in contactPoints)
{
Debug.Log("Contact Point: " + point);
}
}
Last updated