//If we have a rexObject slotted, this will notify it of any collisions and enable it to act accordingly private void NotifyOfCollision(Collider2D col, RexObject.Side side, RexObject.CollisionType collisionType) { if (rexObject != null) { rexObject.OnPhysicsCollision(col, side, collisionType); } RexObject otherObject = col.GetComponent <RexObject>(); if (otherObject != null) { RexObject.Side otherSide; if (side == RexObject.Side.Bottom) { otherSide = RexObject.Side.Top; } else if (side == RexObject.Side.Top) { otherSide = RexObject.Side.Bottom; } else if (side == RexObject.Side.Left) { otherSide = RexObject.Side.Right; } else { otherSide = RexObject.Side.Left; } otherObject.NotifyOfCollisionWithPhysicsObject(boxCol, otherSide, collisionType); } }
//Checks our horizontal collisions, and either stops us from moving or enables us to continue moving private void CheckHorizontalCollisions(float velocityToCheck, bool isTranslatingDirectly = false) { properties.isAgainstLeftWall = false; properties.isAgainstRightWall = false; box = GetRectAtPosition(properties.position); if (velocityToCheck != 0) { //edgebuffer prevents unintended horizontal collisions with a MovingPlatform you're riding on top of float edgeBuffer = (isTranslatingDirectly) ? 0.05f : 0.0f; Vector2 startPoint = new Vector2(box.center.x, box.center.y - box.height * 0.5f + edgeBuffer); Vector2 endPoint = new Vector2(box.center.x, box.center.y + box.height * 0.5f); RaycastHit2D[] raycastHits = new RaycastHit2D[raycastAmounts.horizontal]; int numberOfCollisions = 0; float fraction = 0; float sideRayLength = box.width / 2 + Mathf.Abs(velocityToCheck); Vector2 direction = (velocityToCheck) > 0 ? Vector2.right : -Vector2.right; bool didCollide = false; for (int i = 0; i < raycastAmounts.horizontal; i++) { float raycastSpacing = (float)i / (float)(raycastAmounts.horizontal - 1); Vector2 rayOrigin; if (i == 1 && !willStickToMovingPlatforms) { rayOrigin = Vector2.Lerp(startPoint, endPoint, ((float)(i - 1) / (float)(raycastAmounts.horizontal - 1)) + 0.005f); } else if (i == raycastAmounts.horizontal - 2 && !willStickToMovingPlatforms) { rayOrigin = Vector2.Lerp(startPoint, endPoint, ((float)(i + 1) / (float)(raycastAmounts.horizontal - 1)) - 0.005f); } else { rayOrigin = Vector2.Lerp(startPoint, endPoint, raycastSpacing); } raycastHits[i] = Physics2D.Raycast(rayOrigin, direction, sideRayLength, collisionLayerMask); //Debug.DrawRay(rayOrigin, direction * sideRayLength, Color.red); if (raycastHits[i].fraction > 0) { didCollide = true; if (fraction > 0) { float slopeAngle = Vector2.Angle(raycastHits[i].point - raycastHits[i - 1].point, Vector2.right); if (Mathf.Abs(slopeAngle - 90) < slopeAngleBuffer) //If the slope is too steep, treat it as a wall { if (direction == Vector2.right) { properties.isAgainstRightWall = true; } else { properties.isAgainstLeftWall = true; } if (willStickToMovingPlatforms) { MovingPlatform platform = raycastHits[i].collider.GetComponent <MovingPlatform>(); if (platform != null) { movingPlatform = platform; } else { movingPlatform = null; } } properties.position += (direction * (raycastHits[i].fraction * sideRayLength - box.width / 2.0f)); properties.velocity = new Vector2(0, properties.velocity.y); properties.externalVelocity = new Vector2(0, properties.externalVelocity.y); RexObject.Side side = (direction == Vector2.right) ? RexObject.Side.Right : RexObject.Side.Left; if (side == RexObject.Side.Left && DidHitLeftWallThisFrame() && velocityToCheck < 0.0f) { NotifyOfCollision(raycastHits[i].collider, RexObject.Side.Left, RexObject.CollisionType.Enter); } else if (side == RexObject.Side.Right && DidHitRightWallThisFrame() && velocityToCheck > 0.0f) { NotifyOfCollision(raycastHits[i].collider, RexObject.Side.Right, RexObject.CollisionType.Enter); } break; } } numberOfCollisions++; fraction = raycastHits[i].fraction; } if (!didCollide && isTranslatingDirectly) { properties.position = new Vector2(transform.position.x + velocityToCheck, properties.position.y); } } } else { CheckForWallContact(Direction.Horizontal.Left); CheckForWallContact(Direction.Horizontal.Right); } }