Skip to content

Instantly share code, notes, and snippets.

@piedoom
Last active July 12, 2016 14:20
Show Gist options
  • Save piedoom/164969239e2b4bf047768cb9b27c2f54 to your computer and use it in GitHub Desktop.
Save piedoom/164969239e2b4bf047768cb9b27c2f54 to your computer and use it in GitHub Desktop.
class TestCollider : Component, IUpdatable
{
public override void onAddedToEntity()
{
entity.addCollider<BoxCollider>(new BoxCollider(100,100));
}
public void update()
{
var bottomLeft = new Vector2(entity.colliders.First().bounds.left, entity.colliders.First().bounds.bottom);;
RaycastHit bottomHit = Physics.linecast(bottomLeft, bottomLeft + (Vector2.UnitY * 100));
Debug.drawLine(bottomLeft, bottomLeft + (Vector2.UnitY * 100), Color.Red);
if (bottomHit.collider != null)
{
Debug.log($"hit distance bottom: {bottomHit.distance}");
}
var topRight = new Vector2(entity.colliders.First().bounds.right, entity.colliders.First().bounds.top);
RaycastHit rightHit = Physics.linecast(topRight, topRight + (Vector2.UnitX * 100));
Debug.drawLine(topRight, topRight + (Vector2.UnitX * 100), Color.Red);
if (rightHit.collider != null)
{
Debug.log($"hit distance right: {rightHit.distance}");
}
var position = Input.mousePosition;
if (Input.leftMouseButtonDown)
transform.position = position;
}
}
/// In the scene file
// the raycaster
createEntity("collider-1", new Vector2(100,100)).addComponent<TestCollider>(new TestCollider());
// the obstacle
createEntity("box", new Vector2(300, 300)).addCollider<BoxCollider>(new BoxCollider(100, 100));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment