zero out velocities on collisions

master
Jordan Orelli 5 years ago
parent 3c9bc548c1
commit 483fbf510e

@ -11,6 +11,7 @@ public class MoveController : MonoBehaviour
public int horizontalRayCount = 4; public int horizontalRayCount = 4;
public int verticalRayCount = 4; public int verticalRayCount = 4;
public LayerMask collisionMask; public LayerMask collisionMask;
public CollisionInfo collisions;
private float horizontalRaySpacing; private float horizontalRaySpacing;
private float verticalRaySpacing; private float verticalRaySpacing;
@ -25,6 +26,8 @@ public class MoveController : MonoBehaviour
public void Move(Vector3 velocity) { public void Move(Vector3 velocity) {
UpdateRaycastOrigins(); UpdateRaycastOrigins();
collisions.Reset();
if (velocity.x != 0) { if (velocity.x != 0) {
HorizontalCollisions(ref velocity); HorizontalCollisions(ref velocity);
} }
@ -45,6 +48,11 @@ public class MoveController : MonoBehaviour
Debug.DrawRay(rayOrigin, Vector2.up * directionY * rayLength, Color.red); Debug.DrawRay(rayOrigin, Vector2.up * directionY * rayLength, Color.red);
RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.up * directionY, rayLength, collisionMask); RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.up * directionY, rayLength, collisionMask);
if (hit) { if (hit) {
if (directionY > 0) {
collisions.above = true;
} else {
collisions.below = true;
}
velocity.y = (hit.distance - skinWidth) * directionY; velocity.y = (hit.distance - skinWidth) * directionY;
rayLength = hit.distance; rayLength = hit.distance;
} }
@ -62,6 +70,11 @@ public class MoveController : MonoBehaviour
Debug.DrawRay(rayOrigin, Vector2.right * directionX * rayLength, Color.red); Debug.DrawRay(rayOrigin, Vector2.right * directionX * rayLength, Color.red);
RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.right * directionX, rayLength, collisionMask); RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.right * directionX, rayLength, collisionMask);
if (hit) { if (hit) {
if (directionX > 0) {
collisions.right = true;
} else {
collisions.left = true;
}
velocity.x = (hit.distance - skinWidth) * directionX; velocity.x = (hit.distance - skinWidth) * directionX;
rayLength = hit.distance; rayLength = hit.distance;
} }
@ -99,4 +112,18 @@ public class MoveController : MonoBehaviour
public Vector2 bottomLeft; public Vector2 bottomLeft;
public Vector2 bottomRight; public Vector2 bottomRight;
} }
public struct CollisionInfo {
public bool above;
public bool below;
public bool left;
public bool right;
public void Reset() {
above = false;
below = false;
left = false;
right = false;
}
}
} }

@ -16,6 +16,13 @@ public class PlayerController : MonoBehaviour {
} }
void Update() { void Update() {
if (moveController.collisions.below || moveController.collisions.above) {
velocity.y = 0;
}
if (moveController.collisions.left || moveController.collisions.right) {
velocity.x = 0;
}
Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")); Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
velocity.x = input.x * moveSpeed; velocity.x = input.x * moveSpeed;
velocity.y += gravity * Time.deltaTime; velocity.y += gravity * Time.deltaTime;

Loading…
Cancel
Save