Skip to content

Commit

Permalink
capsule/bullet collider
Browse files Browse the repository at this point in the history
Added a feature to the capsule collider 2D which can transform the
capsule into a bullet
  • Loading branch information
GuyQuad committed Jan 20, 2016
1 parent 0d01b62 commit 79d0965
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 20 deletions.
79 changes: 59 additions & 20 deletions Custom 2D Colliders/Scripts/CapsuleCollider2D.cs
Expand Up @@ -35,6 +35,10 @@
[RequireComponent(typeof(EdgeCollider2D))]
public class CapsuleCollider2D : MonoBehaviour {

[HideInInspector]
public bool bullet = false, flip = false;

[HideInInspector]
[Range(.5f, 25)]
public float radius = 1;

Expand All @@ -48,50 +52,85 @@ public class CapsuleCollider2D : MonoBehaviour {
public int rotation = 0;

Vector2 origin, center, center1, center2;

List<Vector2> points;
float ang = 0;

public Vector2[] getPoints(Vector2 off)
{
List<Vector2> points = new List<Vector2>();
points = new List<Vector2>();

origin = transform.localPosition;
center = origin + off;

float r = (height / 2f) - (radius);

if (bullet && flip) r += radius;

center1.x = center.x + r * Mathf.Sin(rotation * Mathf.Deg2Rad);
center1.y = center.y + r * Mathf.Cos(rotation * Mathf.Deg2Rad);

if (bullet) {
if (!flip) r += radius;
else r -= radius;
}

center2.x = center.x + r * Mathf.Sin((rotation + 180f) * Mathf.Deg2Rad);
center2.y = center.y + r * Mathf.Cos((rotation + 180f) * Mathf.Deg2Rad);

float ang = 360 - rotation;

for (int i = 0; i <= smoothness/2; i++)
{
float a = ang * Mathf.Deg2Rad;
float x = center1.x + radius * Mathf.Cos(a);
float y = center1.y + radius * Mathf.Sin(a);

points.Add(new Vector2(x, y));
ang += 360f/smoothness;
}

ang -= 360f / smoothness;

ang = 360f - rotation;
ang %= 360;

for (int i = smoothness/2; i <= smoothness; i++)
// top semi circle
for (int i = 0; i <= smoothness; i++)
{
float a = ang * Mathf.Deg2Rad;
float x = center2.x + radius * Mathf.Cos(a);
float y = center2.y + radius * Mathf.Sin(a);
if (bullet && flip)
{
calcPointLocation(radius, center1);
ang += 180f;
calcPointLocation(radius, center1);
i = smoothness + 1;
}
else
{
calcPointLocation(radius, center1);
ang += 180f / smoothness;
}
}

ang -= 180f / smoothness;
ang %= 360;

points.Add(new Vector2(x, y));
ang += 360f / smoothness;
// bottom semi circle
for (int i = 0; i <= smoothness; i++)
{
if (bullet && !flip)
{
calcPointLocation(radius, center2);
ang += 180f;
calcPointLocation(radius, center2);
i = smoothness + 1;
}
else
{
calcPointLocation(radius, center2);
ang += 180f / smoothness;
}
}

points.Add(points[0]);

return points.ToArray();
}

void calcPointLocation(float r, Vector2 centerPt)
{
float a = ang * Mathf.Deg2Rad;
float x = centerPt.x + r * Mathf.Cos(a);
float y = centerPt.y + r * Mathf.Sin(a);

points.Add(new Vector2(x, y));
}
}
#endif
Expand Up @@ -35,6 +35,7 @@ public class CapsuleCollider_Editor : Editor {
CapsuleCollider2D capCol;
EdgeCollider2D edgeCollider;
Vector2 off;
bool advanced;

void OnEnable()
{
Expand All @@ -55,6 +56,12 @@ public override void OnInspectorGUI()
DrawDefaultInspector();

capCol.radius = Mathf.Clamp(capCol.radius, 0.5f, capCol.height / 2);
capCol.radius = EditorGUILayout.Slider("Radius", capCol.radius, 0.25f, capCol.height / 2f);

GUILayout.Space(8);
capCol.bullet = EditorGUILayout.Toggle("Bullet", capCol.bullet);
if(capCol.bullet) capCol.flip = EditorGUILayout.Toggle("Flip", capCol.flip);


if (GUI.changed || !off.Equals(edgeCollider.offset))
{
Expand Down
Binary file modified CustomColliders2D.unitypackage
Binary file not shown.

0 comments on commit 79d0965

Please sign in to comment.