Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- RaycastHit2D groundHit;
- float heightOfCast = 2f;
- //origin is at the bottom of the object, just right outside of its box collider
- Vector2 newCastPoint = new Vector2 (transform.position.x, transform.position.y - thisCollider.size.y/2f*transform.localScale.y);
- //heightOfCast is added because the height, just like the width, increases from the center. not bottom or top
- newCastPoint = new Vector2 (newCastPoint.x, newCastPoint.y - heightOfCast);
- //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
- Debug.DrawLine(newCastPoint, new Vector2(newCastPoint.x, newCastPoint.y), Color.green, 0.1f);
- //this is the left corner point of the object
- Vector2 leftCast = new Vector2 (newCastPoint.x - thisCollider.size.x/2f * transform.localScale.x, newCastPoint.y + heightOfCast);
- //this is the right corner point of the object
- Vector2 rightCast = new Vector2 (newCastPoint.x + thisCollider.size.x/2f * transform.localScale.x, newCastPoint.y + heightOfCast);
- //this shows the x size of the cast
- Debug.DrawLine (leftCast, rightCast, Color.green, 0.1f);
- //this shows the y size of the cast
- Debug.DrawLine (new Vector2(leftCast.x, leftCast.y - heightOfCast*1.5f), new Vector2(rightCast.x, rightCast.y - heightOfCast*1.5f), Color.green, 0.5f);
- //this connects both sides of the cast to show u the box created
- Debug.DrawLine(leftCast, new Vector2(leftCast.x, leftCast.y - heightOfCast*1.5f), Color.green, 0.1f);
- Debug.DrawLine(rightCast, new Vector2(rightCast.x, rightCast.y - heightOfCast*1.5f), Color.green, 0.1f);
- groundHit = Physics2D.BoxCast (newCastPoint, new Vector2(Vector2.Distance(leftCast, rightCast), + heightOfCast), 0f, new Vector2 (0f, -1f), Mathf.Infinity, layersToIgnore);
- if (groundHit.collider != null) {
- Debug.Log(groundHit.collider.gameObject.name + ": " + groundHit.distance);
- }
Advertisement
Add Comment
Please, Sign In to add comment