Skip to content

Instantly share code, notes, and snippets.

@caramelchocolate
Last active January 12, 2020 12:31
Show Gist options
  • Save caramelchocolate/af679883f1ebfb2724a6b51a6c3864be to your computer and use it in GitHub Desktop.
Save caramelchocolate/af679883f1ebfb2724a6b51a6c3864be to your computer and use it in GitHub Desktop.
Compute CapsuleCollider2D position(x) center
using UnityEngine;
public class DebugGizmos : MonoBehaviour {
void OnDrawGizmos() {
float halfWidth = GetComponent<SpriteRenderer>().bounds.extents.x;
float halfHeight = GetComponent<SpriteRenderer>().bounds.extents.y;
CapsuleCollider2D capsuleCollider2D = GetComponent<CapsuleCollider2D>();
Vector3 capsuleColliderCenter = transform.position + (transform.right * capsuleCollider2D.offset.x * transform.localScale.x);
Vector3 capsuleColliderBottom = capsuleColliderCenter - (transform.up * halfHeight);
Vector3 capsuleColliderBottomLeft = capsuleColliderBottom - (transform.right * halfWidth);
Vector3 capsuleColliderBottomRight = capsuleColliderBottom + (transform.right * halfWidth);
Gizmos.color = Color.cyan;
// Compute CapsuleCollider2D position(x) center
Gizmos.DrawLine(capsuleColliderCenter, capsuleColliderBottom);
// IsGrounded
Gizmos.DrawLine(capsuleColliderBottomLeft + (transform.right * 0.3f), capsuleColliderBottom - (transform.up * 0.1f));
Gizmos.DrawLine(capsuleColliderBottomRight - (transform.right * 0.3f), capsuleColliderBottom - (transform.up * 0.1f));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment