bool BoxColliderFloorDetection() { float startValue = -0.45f; float endValue = 0.45f; int numOfRays = 10; bool returnAnswer = false; RaycastHit2D hit; for (int i = 0; i < numOfRays + 1; i++) { Vector2 rayStartPos = new Vector2(this.transform.position.x, this.transform.position.y) + new Vector2(startValue + (endValue - startValue) * i / numOfRays, -0.5f); //rayStartPos = rayStartPos.normalized * this.transform.lossyScale.magnitude/2; //print(rayStartPos); hit = Physics2D.Raycast(rayStartPos, Vector2.down, 0.05f, platformLayerMask); Debug.DrawRay(rayStartPos, Vector3.down, Color.red, 0.05f); if (hit.collider != null && hit.collider.tag != "LightWall") //If it hits something that isn't a light wall { BasicPlatformScript script = hit.collider.GetComponentInParent <BasicPlatformScript>(); if (script != null) //If the floor is actually a moving platform, follow along { isOnMovingPlatform = true; //print(script.GetMoveSpeedAndDir()); this.transform.position += script.GetMoveSpeedAndDir() * Time.fixedDeltaTime; //xVthiselocity += script.GetMoveSpeedAndDir().x * Time.fixedDeltaTime; //yVelocity += script.GetMoveSpeedAndDir().y * Time.fixedDeltaTime; } else { isOnMovingPlatform = false; } hit = Physics2D.Raycast(rayStartPos + Vector2.up * 0.5f, Vector2.down, 1f, platformLayerMask); //Used to stop player from clipping into floor if (hit.collider.tag != "LightWall" && !BoxColliderRoofDetection()) //If this doesn't hit a roof, move up slightly { this.transform.position = new Vector3(this.transform.position.x, hit.point.y + 0.5f, this.transform.position.z); } returnAnswer = true; return(returnAnswer); //yVelocity = 0; //break; } } return(returnAnswer); }
bool BoxColliderFloorDetection() { float startValue = -0.45f; float endValue = 0.45f; int numOfRays = 10; bool returnAnswer = false; RaycastHit2D hit; for (int i = 0; i < numOfRays + 1; i++) { Vector2 rayStartPos = new Vector2(this.transform.position.x, this.transform.position.y) + new Vector2(startValue + (endValue - startValue) * i / numOfRays, -0.5f); //rayStartPos = rayStartPos.normalized * this.transform.lossyScale.magnitude/2; //print(rayStartPos); hit = Physics2D.Raycast(rayStartPos, Vector2.down, 0.05f, platformLayerMask); Debug.DrawRay(rayStartPos, Vector3.down, Color.red, 0.05f); if (hit.collider != null && hit.collider.tag != "DarkWall") //If it hits something that isn't a dark wall { BasicPlatformScript script = hit.collider.GetComponentInParent <BasicPlatformScript>(); if (script != null) //If the floor is actually a moving platform, follow along { print(script.GetMoveSpeedAndDir()); this.transform.position += script.GetMoveSpeedAndDir() * Time.fixedDeltaTime; //xVthiselocity += script.GetMoveSpeedAndDir().x * Time.fixedDeltaTime; //yVelocity += script.GetMoveSpeedAndDir().y * Time.fixedDeltaTime; } //print(hit.collider.name); hit = Physics2D.Raycast(rayStartPos + Vector2.up * 0.5f, Vector2.down, 0.6f, platformLayerMask); //Check the point at which the floor is hit if (hit.collider.tag != "DarkWall" && !BoxColliderRoofDetection()) //If it hits the platform AND the character is not touching the roof { this.transform.position = new Vector3(this.transform.position.x, hit.point.y + 0.5f, this.transform.position.z); } return(true); //yVelocity = 0; //break; } } return(returnAnswer); }