/// <summary> /// Will fire off rays in the direction that it is traveling /// </summary> protected virtual void checkVerticalRays() { Vector2 left = allCorners.bottomLeft; Vector2 right = allCorners.bottomRight; if (rigid.velocity.y > 0) { left = allCorners.topLeft; right = allCorners.topRight; } left.x += verticalOffset; right.x -= verticalOffset; Ray2D ray = new Ray2D(); rigid.inAir = true; for (int i = 0; i < verticalRayTraceCount; i++) { ray.origin = left + ((right - left) / (verticalRayTraceCount - 1)) * i; ray.direction = rigid.velocity.y <= 0 ? Vector2.down : Vector2.up; RaycastHit2D hit; float distanctToCalculate = Mathf.Abs(rigid.velocity.y) * Time.deltaTime + (rigid.velocity.y == 0f ? Mathf.Abs(horizontalOffset) * 2 + .01f : 0); hit = Physics2D.Raycast(ray.origin, ray.direction, distanctToCalculate, LayerMask.GetMask(layerMask)); if (hit) { //print("I hit a thing"); GroundColliders groundCollider = hit.collider.GetComponent <GroundColliders>(); if (groundCollider) { Vector2 collisionPoint; if (rigid.velocity.y <= 0) { collisionPoint = groundCollider.getTopPosition(transform.position.x); transform.position = new Vector3(collisionPoint.x, collisionPoint.y, transform.position.z); } else { collisionPoint = groundCollider.getBottomPosition(transform.position.x); } rigid.inAir = false; rigid.velocity.y = 0; } } if (DebugSettings.Instance.displayColliderRays) { DebugSettings.DrawDebugRay(ray.origin, ray.origin + (ray.direction * distanctToCalculate)); //DebugSettings.DrawDebugRay(ray.origin, ray.origin + Vector2.down * .2f); } } //print(rigid.inAir); }
protected virtual void checkHorizontalRays() { Vector2 top = allCorners.topRight; Vector2 bottom = allCorners.bottomRight; float xVel = rigid.velocity.x; if (xVel < 0) { top = allCorners.topLeft; bottom = allCorners.bottomLeft; } top.y -= horizontalOffset; bottom.y += horizontalOffset; Ray2D ray = new Ray2D(); for (int i = 0; i < horizontalRayTraceCount; i++) { ray.origin = bottom + ((top - bottom) / (horizontalRayTraceCount - 1)) * i; ray.direction = (Vector2.right * xVel).normalized; RaycastHit2D hit; hit = Physics2D.Raycast(ray.origin, ray.direction, Mathf.Abs(xVel) * Time.deltaTime, LayerMask.GetMask(layerMask)); if (hit) { GroundColliders groundCollider = hit.collider.GetComponent <GroundColliders>(); if (groundCollider) { Vector2 collisionPoint; if ((rigid.velocity.x) < 0) { //print("Left"); collisionPoint = groundCollider.getRightPosition(transform.position.y); collisionPoint = collisionPoint - new Vector2(Mathf.Abs(transform.position.x - allCorners.bottomLeft.x), 0); } else { //print("Right"); collisionPoint = groundCollider.getLeftPosition(transform.position.y); collisionPoint = collisionPoint + new Vector2(Mathf.Abs(transform.position.x - allCorners.bottomRight.x), 0); } //print(collisionPoint); //We are definitely coming back to this. This is too stupid to actually work consistently.... //transform.position = new Vector3(collisionPoint.x, collisionPoint.y, transform.position.z); rigid.velocity.x = 0;//If we hit a wall, then more than likely we will be setting the velocity to 0 } } if (DebugSettings.Instance.displayColliderRays) { DebugSettings.DrawDebugRay(ray.origin, ray.origin + ray.direction * Mathf.Abs(xVel) * Time.deltaTime); } } }