You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
4.4 KiB
C#

5 years ago
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// MoveController controls movement for 2D characters
[RequireComponent(typeof(BoxCollider2D))]
5 years ago
public class MoveController : MonoBehaviour
{
const float skinWidth = 0.015f;
5 years ago
public int horizontalRayCount = 4;
public int verticalRayCount = 4;
public LayerMask collisionMask;
public CollisionInfo collisions;
5 years ago
private float horizontalRaySpacing;
private float verticalRaySpacing;
new BoxCollider2D collider;
RaycastOrigins raycastOrigins;
5 years ago
public bool isGrounded {
get => collisions.below;
}
5 years ago
void Start() {
collider = GetComponent<BoxCollider2D>();
CalculateRaySpacing();
5 years ago
}
public void Move(Vector3 velocity) {
5 years ago
UpdateRaycastOrigins();
collisions.Reset();
if (velocity.x != 0) {
HorizontalCollisions(ref velocity);
}
if (velocity.y != 0) {
VerticalCollisions(ref velocity);
}
transform.Translate(velocity);
}
private void VerticalCollisions(ref Vector3 velocity) {
float directionY = Mathf.Sign(velocity.y); // -1 for down, 1 for up
float rayLength = Mathf.Abs(velocity.y) + skinWidth;
5 years ago
for (int i = 0; i < verticalRayCount; i++) {
Vector2 rayOrigin = (directionY > 0) ? raycastOrigins.topLeft : raycastOrigins.bottomLeft;
rayOrigin += Vector2.right * (verticalRaySpacing * i + velocity.x);
Debug.DrawRay(rayOrigin, Vector2.up * directionY * rayLength, Color.red);
RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.up * directionY, rayLength, collisionMask);
if (hit) {
if (directionY > 0) {
5 years ago
collisions.above = hit.collider;
} else {
5 years ago
collisions.below = hit.collider;
}
velocity.y = (hit.distance - skinWidth) * directionY;
rayLength = hit.distance;
}
}
}
private void HorizontalCollisions(ref Vector3 velocity) {
float directionX = Mathf.Sign(velocity.x); // -1 for left, 1 for right
float rayLength = Mathf.Abs(velocity.x) + skinWidth;
for (int i = 0; i < horizontalRayCount; i++) {
Vector2 rayOrigin = (directionX > 0) ? raycastOrigins.bottomRight : raycastOrigins.bottomLeft;
rayOrigin += Vector2.up * (horizontalRaySpacing * i);
Debug.DrawRay(rayOrigin, Vector2.right * directionX * rayLength, Color.red);
RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.right * directionX, rayLength, collisionMask);
if (hit) {
if (directionX > 0) {
5 years ago
collisions.right = hit.collider;
} else {
5 years ago
collisions.left = hit.collider;
}
velocity.x = (hit.distance - skinWidth) * directionX;
rayLength = hit.distance;
}
5 years ago
}
}
void UpdateRaycastOrigins() {
Bounds bounds = collider.bounds;
bounds.Expand(skinWidth * -2f);
5 years ago
raycastOrigins.bottomLeft = new Vector2(bounds.min.x, bounds.min.y);
raycastOrigins.bottomRight = new Vector2(bounds.max.x, bounds.min.y);
raycastOrigins.topLeft = new Vector2(bounds.min.x, bounds.max.y);
raycastOrigins.topRight = new Vector2(bounds.max.x, bounds.max.y);
5 years ago
}
void CalculateRaySpacing() {
Bounds bounds = collider.bounds;
bounds.Expand(skinWidth * -2);
if (horizontalRayCount < 2) {
horizontalRayCount = 2;
}
if (verticalRayCount < 2) {
verticalRayCount = 2;
}
5 years ago
horizontalRaySpacing = bounds.size.y / (horizontalRayCount - 1);
verticalRaySpacing = bounds.size.x / (verticalRayCount - 1);
}
struct RaycastOrigins {
public Vector2 topLeft;
public Vector2 topRight;
public Vector2 bottomLeft;
public Vector2 bottomRight;
5 years ago
}
public struct CollisionInfo {
5 years ago
public Collider2D above;
public Collider2D below;
public Collider2D left;
public Collider2D right;
public void Reset() {
5 years ago
above = null;
below = null;
left = null;
right = null;
}
}
5 years ago
}