private void OnCollisionEnter(Collision collision) { this.DebugCollision(true, collision); // when colliding with a potential ground. foreach (ContactPoint pt in collision.contacts) { // if the normal direction has something to do with gravity - it's good enough to be a ground! float dotCosinus = Mover.CalculateDotProductCosine(Physics.gravity, pt.normal); if (dotCosinus < 0f) // => the normal of the collision has a force that goes against gravity. { this.IsGrounded = true; this._ground = collision.collider; break; // no reason to look for other grounds..? } } }
void DebugCollision(bool enter, Collision collision) { string e = "exit"; Color line = Color.blue; if (enter) { e = "enter"; line = Color.red; } foreach (ContactPoint pt in collision.contacts) { float cos = Mover.CalculateDotProductCosine( Physics.gravity, pt.normal, out float dot ); print($"{this.name}: [{e}] {collision.collider.name} -> normal: {pt.normal}; dot: {dot}; cos: {cos}"); Debug.DrawLine(pt.point, pt.point + pt.normal, line, 1f); } }
public static float CalculateDotProductCosine(Vector3 one, Vector3 another) { return(Mover.CalculateDotProductCosine(one, another, out float dot)); }