Skip to content

Instantly share code, notes, and snippets.

@eduardonunesp
Created September 3, 2019 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eduardonunesp/cb0244d9c1c600d8df9c61117ab187bf to your computer and use it in GitHub Desktop.
Save eduardonunesp/cb0244d9c1c600d8df9c61117ab187bf to your computer and use it in GitHub Desktop.
Detect Side Collision
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag.Equals("Wall"))
{
Vector3 hit = col.contacts[0].normal;
Debug.Log(hit);
float angle = Vector3.Angle(hit, Vector3.up);
if (Mathf.Approximately(angle, 0))
{
//Down
Debug.Log("Down");
}
if(Mathf.Approximately(angle, 180))
{
//Up
Debug.Log("Up");
}
if(Mathf.Approximately(angle, 90)){
// Sides
Vector3 cross = Vector3.Cross(Vector3.forward, hit);
if (cross.y > 0)
{ // left side of the player
Debug.Log("Left");
}
else
{ // right side of the player
Debug.Log("Right");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment