using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour{
private float topAngle;
private float sideAngle;
void Start() {
Vector2 size = GetComponent<BoxCollider2D>().size;
size = Vector2.Scale (size, (Vector2)transform.localScale);
topAngle = Mathf.Atan (size.x / size.y) * Mathf.Rad2Deg;
sideAngle = 90.0f - topAngle;
Debug.Log (topAngle + ", " + sideAngle);
}
void OnCollisionEnter2D(Collision2D coll) {
Vector3 v = (Vector3)coll.contacts[0].point - transform.position;
if (Vector3.Angle(v, transform.up) <= topAngle) {
Debug.Log("Collision on top");
}
else if (Vector3.Angle(v, transform.right) <= sideAngle) {
Debug.Log("Collision on right");
}
else if (Vector3.Angle(v, -transform.right) <= sideAngle) {
Debug.Log ("Collision on Left");
}
else {
Debug.Log("Collision on Bottom");
}
}
}
thanks! The line: Vector3 v = coll.contacts[0].point - transform.position; return an exception: The calls is ambiguous between the following methods or properties. Do you know what this ?
thanks! The line:
– ferpaVector3 v = coll.contacts[0].point - transform.position;return an exception:The calls is ambiguous between the following methods or properties. Do you know what this ?thanks a lot, now works !
– ferpaMy original idea had a couple of flaws. First, the box collider had to be square, and second, it did not handle rotation. I edited my answer above.
– robertbuvery nice, thanks a lot !
– ferpa