diff --git a/Assets/Scripts/Player/CollisionState.cs b/Assets/Scripts/Player/CollisionState.cs index 74966f3ed06f8ec5e93da1d0b40c7d1ff3f4a106..61487e3d2bc32b9318cb6b9b7474e5b923f95f06 100644 --- a/Assets/Scripts/Player/CollisionState.cs +++ b/Assets/Scripts/Player/CollisionState.cs @@ -7,6 +7,7 @@ public class CollisionState : MonoBehaviour { public LayerMask enemyLayer; public float skinWidth = 0; public float groundCheckDistance = 0.1f; + public float wallCheckDistance = 0.1f; public bool onGround { get; private set; } public bool onWallLeft { get; private set; } @@ -37,46 +38,50 @@ public class CollisionState : MonoBehaviour { void FixedUpdate() { // TODO: enemy hit stuff + RaycastHit2D hit; + onGround = CheckCollision(Vector2.down, groundCheckDistance); + onWallLeft = CheckCollision(Vector2.left, wallCheckDistance); + onWallRight = CheckCollision(Vector2.right, wallCheckDistance); + } - RaycastHit2D hit = Physics2D.CircleCast( - circle.bounds.center, // center - circle.radius - skinWidth, // radius - Vector2.down, // direction - groundCheckDistance, // distance - collisionLayer // layermask - ); - onGround = hit.collider != null; - + bool CheckCollision(Vector2 direction, float range) { + return Physics2D.CircleCast( + circle.bounds.center, // center + circle.radius - skinWidth, // radius + direction, // direction + range, // distance + collisionLayer // layermask + ).collider != null; } float lastFixedTime; - void OnCollisionStay2D(Collision2D collision) { - if (Time.fixedTime != lastFixedTime) { - // only reset flags at the start of each frame - // (OnCollisionStay2D is called once for every collider this body is in contact with) - onWallLeft = false; - onWallRight = false; - lastFixedTime = Time.fixedTime; - } - foreach (ContactPoint2D contact in collision.contacts) { - Color color = Color.white; - Vector2 normal = contact.normal; - if (normal == Vector2.right) { - color = Color.red; - onWallLeft = true; - } - if (normal == -Vector2.right) { - color = Color.green; - onWallRight = true; - } - Debug.DrawRay(contact.point, contact.normal, color); - } - } + // void OnCollisionStay2D(Collision2D collision) { + // if (Time.fixedTime != lastFixedTime) { + // // only reset flags at the start of each frame + // // (OnCollisionStay2D is called once for every collider this body is in contact with) + // onWallLeft = false; + // onWallRight = false; + // lastFixedTime = Time.fixedTime; + // } + // foreach (ContactPoint2D contact in collision.contacts) { + // Color color = Color.white; + // Vector2 normal = contact.normal; + // if (normal == Vector2.right) { + // color = Color.red; + // onWallLeft = true; + // } + // if (normal == -Vector2.right) { + // color = Color.green; + // onWallRight = true; + // } + // Debug.DrawRay(contact.point, contact.normal, color); + // } + // } - void OnCollisionExit2D() { - // reset flags when we arent colliding with anything at all - onWallLeft = false; - onWallRight = false; - } + // void OnCollisionExit2D() { + // // reset flags when we arent colliding with anything at all + // onWallLeft = false; + // onWallRight = false; + // } } diff --git a/Assets/Scripts/Player/Walk.cs b/Assets/Scripts/Player/Walk.cs index 7e81a335286f9438f70725eb45c53910be7a7388..fb5f5bc375120ae19d77163c740d9f9e33cb0dd2 100644 --- a/Assets/Scripts/Player/Walk.cs +++ b/Assets/Scripts/Player/Walk.cs @@ -150,7 +150,7 @@ public class Walk : AbstractBehavior { //slow down y velocity if(body.velocity.y < clingSpeed) { - body.velocity = new Vector2(body.velocity.x, -clingSpeed); + body.velocity = new Vector2(0, -clingSpeed); } }