Guest User

Untitled

a guest
Nov 21st, 2015
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.85 KB | None | 0 0
  1. RaycastHit2D groundHit;
  2.  
  3. float heightOfCast = 2f;
  4. //origin is at the bottom of the object, just right outside of its box collider
  5. Vector2 newCastPoint = new Vector2 (transform.position.x, transform.position.y - thisCollider.size.y/2f*transform.localScale.y);
  6. //heightOfCast is added because the height, just like the width, increases from the center. not bottom or top
  7. newCastPoint = new Vector2 (newCastPoint.x, newCastPoint.y - heightOfCast);
  8. //this shows that newCastPoint is indeed right outside of the objects collider, with a height of -0.1f (this should be the y size of the boxcast
  9. Debug.DrawLine(newCastPoint, new Vector2(newCastPoint.x, newCastPoint.y), Color.green, 0.1f);
  10. //this is the left corner point of the object
  11. Vector2 leftCast = new Vector2 (newCastPoint.x - thisCollider.size.x/2f * transform.localScale.x, newCastPoint.y + heightOfCast);
  12. //this is the right corner point of the object
  13. Vector2 rightCast = new Vector2 (newCastPoint.x + thisCollider.size.x/2f * transform.localScale.x, newCastPoint.y + heightOfCast);
  14. //this shows the x size of the cast
  15. Debug.DrawLine (leftCast, rightCast, Color.green, 0.1f);
  16. //this shows the y size of the cast
  17. Debug.DrawLine (new Vector2(leftCast.x, leftCast.y - heightOfCast*1.5f), new Vector2(rightCast.x, rightCast.y - heightOfCast*1.5f), Color.green, 0.5f);
  18. //this connects both sides of the cast to show u the box created
  19. Debug.DrawLine(leftCast, new Vector2(leftCast.x, leftCast.y - heightOfCast*1.5f), Color.green, 0.1f);
  20. Debug.DrawLine(rightCast, new Vector2(rightCast.x, rightCast.y - heightOfCast*1.5f), Color.green, 0.1f);
  21. groundHit = Physics2D.BoxCast (newCastPoint, new Vector2(Vector2.Distance(leftCast, rightCast), + heightOfCast), 0f, new Vector2 (0f, -1f), Mathf.Infinity, layersToIgnore);
  22. if (groundHit.collider != null) {
  23. Debug.Log(groundHit.collider.gameObject.name + ": " + groundHit.distance);
  24. }
Advertisement
Add Comment
Please, Sign In to add comment