Commit a76b053a authored by Tanner Grehawick's avatar Tanner Grehawick

Merge branch 'character-wallslide-fix'

parents 4f5c0d00 222a2258
...@@ -7,6 +7,7 @@ public class CollisionState : MonoBehaviour { ...@@ -7,6 +7,7 @@ public class CollisionState : MonoBehaviour {
public LayerMask enemyLayer; public LayerMask enemyLayer;
public float skinWidth = 0; public float skinWidth = 0;
public float groundCheckDistance = 0.1f; public float groundCheckDistance = 0.1f;
public float wallCheckDistance = 0.1f;
public bool onGround { get; private set; } public bool onGround { get; private set; }
public bool onWallLeft { get; private set; } public bool onWallLeft { get; private set; }
...@@ -37,46 +38,50 @@ public class CollisionState : MonoBehaviour { ...@@ -37,46 +38,50 @@ public class CollisionState : MonoBehaviour {
void FixedUpdate() { void FixedUpdate() {
// TODO: enemy hit stuff // 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( bool CheckCollision(Vector2 direction, float range) {
circle.bounds.center, // center return Physics2D.CircleCast(
circle.radius - skinWidth, // radius circle.bounds.center, // center
Vector2.down, // direction circle.radius - skinWidth, // radius
groundCheckDistance, // distance direction, // direction
collisionLayer // layermask range, // distance
); collisionLayer // layermask
onGround = hit.collider != null; ).collider != null;
} }
float lastFixedTime; float lastFixedTime;
void OnCollisionStay2D(Collision2D collision) { // void OnCollisionStay2D(Collision2D collision) {
if (Time.fixedTime != lastFixedTime) { // if (Time.fixedTime != lastFixedTime) {
// only reset flags at the start of each frame // // only reset flags at the start of each frame
// (OnCollisionStay2D is called once for every collider this body is in contact with) // // (OnCollisionStay2D is called once for every collider this body is in contact with)
onWallLeft = false; // onWallLeft = false;
onWallRight = false; // onWallRight = false;
lastFixedTime = Time.fixedTime; // lastFixedTime = Time.fixedTime;
} // }
foreach (ContactPoint2D contact in collision.contacts) { // foreach (ContactPoint2D contact in collision.contacts) {
Color color = Color.white; // Color color = Color.white;
Vector2 normal = contact.normal; // Vector2 normal = contact.normal;
if (normal == Vector2.right) { // if (normal == Vector2.right) {
color = Color.red; // color = Color.red;
onWallLeft = true; // onWallLeft = true;
} // }
if (normal == -Vector2.right) { // if (normal == -Vector2.right) {
color = Color.green; // color = Color.green;
onWallRight = true; // onWallRight = true;
} // }
Debug.DrawRay(contact.point, contact.normal, color); // Debug.DrawRay(contact.point, contact.normal, color);
} // }
} // }
void OnCollisionExit2D() { // void OnCollisionExit2D() {
// reset flags when we arent colliding with anything at all // // reset flags when we arent colliding with anything at all
onWallLeft = false; // onWallLeft = false;
onWallRight = false; // onWallRight = false;
} // }
} }
...@@ -150,7 +150,7 @@ public class Walk : AbstractBehavior { ...@@ -150,7 +150,7 @@ public class Walk : AbstractBehavior {
//slow down y velocity //slow down y velocity
if(body.velocity.y < clingSpeed) if(body.velocity.y < clingSpeed)
{ {
body.velocity = new Vector2(body.velocity.x, -clingSpeed); body.velocity = new Vector2(0, -clingSpeed);
} }
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment