public static Collider CheckCollision(Collider actual, Collider other) { if (actual == other) { return(null); } if (actual.GetColliderType() == 0 && other.GetColliderType() == 0) { if (Vector3.Magnitude(actual._center - other._center) <= (actual.GetComponent <SphereCollider>().GetRadius() + other.GetComponent <SphereCollider>().GetRadius())) { return(other); } } else if (actual.GetColliderType() == 0 && other.GetColliderType() == 1) { var boxHalf = other.GetComponent <BoxCollider>().GetSize() / 2.0f; var distance = actual.GetComponent <SphereCollider>().GetCenter() - other.GetComponent <BoxCollider>().GetCenter(); var cut = Cut(distance, -boxHalf, boxHalf); var next = other.GetComponent <BoxCollider>().GetCenter() + cut; var difference = next - actual.GetComponent <SphereCollider>().GetCenter(); if (Vector3.Magnitude(difference) <= actual.GetComponent <SphereCollider>().GetRadius()) { return(other); } } else if (actual.GetColliderType() == 1 && other.GetColliderType() == 0) { var boxHalf = actual.GetComponent <BoxCollider>().GetSize() / 2.0f; var distance = other.GetComponent <SphereCollider>().GetCenter() - actual.GetComponent <BoxCollider>().GetCenter(); var cut = Cut(distance, -boxHalf, boxHalf); var next = actual.GetComponent <BoxCollider>().GetCenter() + cut; var difference = next - other.GetComponent <SphereCollider>().GetCenter(); if (Vector3.Magnitude(difference) <= other.GetComponent <SphereCollider>().GetRadius()) { return(other); } } else { var actualMin = actual.GetComponent <BoxCollider>().GetMin(); var actualMax = actual.GetComponent <BoxCollider>().GetMax(); var otherMin = other.GetComponent <BoxCollider>().GetMin(); var otherMax = other.GetComponent <BoxCollider>().GetMax(); if (actualMin.X >= otherMin.X && actualMin.Y >= otherMin.Y && actualMin.Z >= otherMin.Z && actualMin.X <= otherMax.X && actualMin.Y <= otherMax.Y && actualMin.Z <= otherMax.Z) { print("Collision of type Box with Box:" + actual.gameObject.name + " " + other.gameObject.name); return(other); } else if (actualMax.X >= otherMin.X && actualMax.Y >= otherMin.Y && actualMax.Z >= otherMax.Z && actualMax.X <= otherMax.X && actualMax.Y <= otherMax.Y && actualMax.Z <= otherMax.Z) { print("Collision of type Box with Box:" + actual.gameObject.name + " " + other.gameObject.name); return(other); } } return(null); }
private void Update() { velocity += Gravity * fixedDeltaTime * (UseGravity ? 1 : 0); if (!IsKinematic) { velocity *= (1 - (Drag * fixedDeltaTime)); } velocity = (Vector3.Right * velocity.X * (X ? 0 : 1)) + (Vector3.Up * velocity.Y * (Y ? 0 : 1)) + (Vector3.Forward * velocity.Z * (Z ? 0 : 1)); if (CheckCollision(Vector3.Up * velocity.Y * deltaTime) != null) { if (collider.GetPhysicsMaterial() != null) { velocity = collider.GetPhysicsMaterial().CalculateFriction(velocity, Vector3.Zero, Vector3.Magnitude(Gravity) * Mass, Mass); } if (CheckCollision(Vector3.Up * velocity.Y * deltaTime).GetCenter().Y + CheckCollision(Vector3.Up * velocity.Y * deltaTime).GetSize().Y <= collider.GetCenter().Y) { velocity.Y = 0; } } position += velocity * deltaTime; pos = position.ToUnity(); this.transform.position = pos; }