private void OnCollisionEnter2D(Collision2D collision) { if (ControllerScript.Instance.GameOver) { return; } var contactPoints = new ContactPoint2D[collision.contactCount]; collision.GetContacts(contactPoints); float collisionNormalX = Mathf.Round(contactPoints[0].normal.x); float collisionNormalY = Mathf.Round(contactPoints[0].normal.y); var collisionNormal = new Vector2(collisionNormalX, collisionNormalY); if (collisionNormal == Vector2.right) { status = PlayerStatus.StuckLeft; crawlDirection = CrawlDirection.Up; } else if (collisionNormal == Vector2.left) { status = PlayerStatus.StuckRight; crawlDirection = CrawlDirection.Up; } else if (collisionNormal == Vector2.up) { status = PlayerStatus.StuckBottom; crawlDirection = CrawlDirection.Left; } else if (collisionNormal == Vector2.down) { status = PlayerStatus.StuckTop; crawlDirection = CrawlDirection.Right; } rigidbody.gravityScale = 0; rigidbody.velocity = Vector2.zero; }
// Update is called once per frame void Update() { if (Input.GetKey(KeyCode.Space) || Input.touches.GetLength(0) > 0) { buttonWasPressed = true; Vector3 rayStart = new Vector3(); Vector3 rayStart2 = new Vector3(); Vector2 rayDirection = new Vector2(); RaycastHit2D hit; RaycastHit2D hit2; switch (status) { case PlayerStatus.Airborne: break; case PlayerStatus.StuckLeft: rayStart = PlayerCenter + new Vector3(-PlayerExtents.x, PlayerExtents.y); rayStart2 = PlayerCenter + new Vector3(-PlayerExtents.x, -PlayerExtents.y); rayDirection = Vector2.left; hit = Physics2D.Raycast(rayStart, rayDirection, .2f); hit2 = Physics2D.Raycast(rayStart2, rayDirection, .2f); Debug.DrawRay(rayStart, rayDirection, Color.red, 0, false); Debug.DrawRay(rayStart2, rayDirection, Color.red, 0, false); if (hit.collider == null && hit2.collider == null) { if (crawlDirection == CrawlDirection.Down) { status = PlayerStatus.StuckTop; } else { status = PlayerStatus.StuckBottom; } crawlDirection = CrawlDirection.Left; } break; case PlayerStatus.StuckRight: rayStart = PlayerCenter + new Vector3(PlayerExtents.x, PlayerExtents.y); rayStart2 = PlayerCenter + new Vector3(PlayerExtents.x, -PlayerExtents.y); rayDirection = Vector2.right; hit = Physics2D.Raycast(rayStart, rayDirection, .2f); hit2 = Physics2D.Raycast(rayStart2, rayDirection, .2f); Debug.DrawRay(rayStart, rayDirection, Color.red, 0, false); Debug.DrawRay(rayStart2, rayDirection, Color.red, 0, false); if (hit.collider == null && hit2.collider == null) { if (crawlDirection == CrawlDirection.Down) { status = PlayerStatus.StuckTop; } else { status = PlayerStatus.StuckBottom; } crawlDirection = CrawlDirection.Right; } break; case PlayerStatus.StuckTop: rayStart = PlayerCenter + new Vector3(-PlayerExtents.x, PlayerExtents.y); rayStart2 = PlayerCenter + new Vector3(PlayerExtents.x, PlayerExtents.y); rayDirection = Vector2.up; hit = Physics2D.Raycast(rayStart, rayDirection, .2f); hit2 = Physics2D.Raycast(rayStart2, rayDirection, .2f); Debug.DrawRay(rayStart, rayDirection, Color.red, 0, false); Debug.DrawRay(rayStart2, rayDirection, Color.red, 0, false); if (hit.collider == null && hit2.collider == null) { if (crawlDirection == CrawlDirection.Right) { status = PlayerStatus.StuckLeft; } else { status = PlayerStatus.StuckRight; } crawlDirection = CrawlDirection.Up; } break; case PlayerStatus.StuckBottom: rayStart = PlayerCenter + new Vector3(-PlayerExtents.x, -PlayerExtents.y); rayStart2 = PlayerCenter + new Vector3(PlayerExtents.x, -PlayerExtents.y); rayDirection = Vector2.down; hit = Physics2D.Raycast(rayStart, rayDirection, .2f); hit2 = Physics2D.Raycast(rayStart2, rayDirection, .2f); Debug.DrawRay(rayStart, rayDirection, Color.red, 0, false); Debug.DrawRay(rayStart2, rayDirection, Color.red, 0, false); if (hit.collider == null && hit2.collider == null) { if (crawlDirection == CrawlDirection.Right) { status = PlayerStatus.StuckLeft; } else { status = PlayerStatus.StuckRight; } crawlDirection = CrawlDirection.Down; } break; default: break; } transform.position += movement[crawlDirection]; } else if (buttonWasPressed) { Vector2 jumpVector = Vector2.zero; switch (status) { case PlayerStatus.Airborne: break; case PlayerStatus.StuckLeft: jumpVector = new Vector2(jumpForceX, jumpForceY); break; case PlayerStatus.StuckRight: jumpVector = new Vector2(-jumpForceX, jumpForceY); break; case PlayerStatus.StuckTop: // just let the player drop? break; case PlayerStatus.StuckBottom: if (crawlDirection == CrawlDirection.Right) { jumpVector = new Vector2(jumpForceX, jumpForceY); } else { jumpVector = new Vector2(-jumpForceX, jumpForceY); } break; default: break; } status = PlayerStatus.Airborne; rigidbody.AddForce(jumpVector); rigidbody.gravityScale = 1; buttonWasPressed = false; } }
private Point CheckDirection(Point ptCurrent, CrawlDirection crawlDirection, out bool bSuccess) { try { Point ptNext = Point.Empty; switch (crawlDirection) { case CrawlDirection.North: if (ptCurrent.Y - 1 < 0) { bSuccess = false; return ptNext; } ptNext = new Point(ptCurrent.X, ptCurrent.Y - 2); break; case CrawlDirection.NorthEast: if (ptCurrent.Y - 1 < 0 || ptCurrent.X + 2 > bmpSource.Width) { bSuccess = false; return ptNext; } ptNext = new Point(ptCurrent.X + 1, ptCurrent.Y - 1); break; case CrawlDirection.East: if (ptCurrent.X + 1 > bmpSource.Width) { bSuccess = false; return ptNext; } ptNext = new Point(ptCurrent.X + 1, ptCurrent.Y); break; case CrawlDirection.SouthEast: if (ptCurrent.Y + 1 > bmpSource.Height || ptCurrent.X + 1 > bmpSource.Width) { bSuccess = false; return ptNext; } ptNext = new Point(ptCurrent.X + 1, ptCurrent.Y + 1); break; case CrawlDirection.South: if (ptCurrent.Y + 2 > bmpSource.Height) { bSuccess = false; return ptNext; } ptNext = new Point(ptCurrent.X, ptCurrent.Y + 2); break; case CrawlDirection.SouthWest: if (ptCurrent.Y + 1 > bmpSource.Height || ptCurrent.X - 1 < bmpSource.Width) { bSuccess = false; return ptNext; } ptNext = new Point(ptCurrent.X - 1, ptCurrent.Y + 1); break; case CrawlDirection.West: if (ptCurrent.X - 1 < 0) { bSuccess = false; return ptNext; } ptNext = new Point(ptCurrent.X - 1, ptCurrent.Y); break; case CrawlDirection.NorthWest: if (ptCurrent.Y - 1 < 0 || ptCurrent.X - 1 < bmpSource.Width) { bSuccess = false; return ptNext; } ptNext = new Point(ptCurrent.X - 1, ptCurrent.Y - 1); break; } Color cPixel = bmpSource.GetPixel(ptNext.X, ptNext.Y); if ((cPixel.R == cToMatch.R && cPixel.G == cToMatch.G && cPixel.B == cToMatch.B)) { bSuccess = true; return ptNext; } else { bSuccess = false; return ptNext; } } catch (StackOverflowException) { bSuccess = false; return Point.Empty; } }