private void FixedUpdate() { Utility.RaycastAnswer rca = coll2d.CastAnswer(Vector2.zero, 0, true); for (int i = 0; i < rca.count; i++) { RaycastHit2D rch2d = rca.rch2ds[i]; if (!rch2d.collider.isTrigger) { Rigidbody2D rb2d = rch2d.collider.GetComponent <Rigidbody2D>(); if (rb2d) { float speed = rb2d.velocity.magnitude; if (speed >= minSpeed) { Vector2 dir = rb2d.velocity.normalized; if (speed > maxSpeed) { rb2d.velocity = dir * maxSpeed; } float durationLeft = (speed - minSpeed) / (maxSpeed - minSpeed); durationLeft = Mathf.Max(durationLeft, 1); rb2d.velocity = Vector2.Lerp( rb2d.velocity, dir * minSpeed, Time.deltaTime / durationLeft ); } } } } }
/// <summary> /// Returns true if there is ground in the given direction relative to Merky /// </summary> /// <param name="direction">The direction to check for ground in</param> /// <returns>True if there is ground in the given direction</returns> public bool isGroundedInDirection(Vector3 direction, float distance = -1) { if (distance < 0) { distance = groundTestDistance; } //Find objects in the given direction Utility.RaycastAnswer answer; answer = coll2d.CastAnswer(direction, distance, true); //Process the found objects for (int i = 0; i < answer.count; i++) { RaycastHit2D rch2d = answer.rch2ds[i]; //If the object is a solid object, if (!rch2d.collider.isTrigger && !rch2d.collider.gameObject.isPlayer()) { //If the object is not a hazard or is not currently hazardous, Hazard hazard = rch2d.collider.gameObject.GetComponent <Hazard>(); if (!hazard || !hazard.Hazardous) { //Then there is ground in the given direction return(true); } } } //Else, There is no ground in the given direction return(false); }
/// <summary> /// Returns true if the two colliders overlap /// </summary> /// <param name=""></param> /// <param name="other"></param> /// <returns></returns> public static bool OverlapsCollider(this Collider2D coll, Collider2D other) { //If the bounds don't overlap if (!coll.bounds.Intersects(other.bounds)) { //then these colliders definitely don't overlap return(false); } //Cast the first one to find if it has it RaycastAnswer answer = coll.CastAnswer(Vector2.zero, 0, false); for (int i = 0; i < answer.count; i++) { if (answer.rch2ds[i].collider == other) { return(true); } } return(false); }