Created
September 3, 2019 21:49
-
-
Save eduardonunesp/cb0244d9c1c600d8df9c61117ab187bf to your computer and use it in GitHub Desktop.
Detect Side Collision
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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