Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- [RequireComponent(typeof(BoxCollider2D))]
- public class EntityController : MonoBehaviour
- {
- // Start is called before the first frame update
- [HideInInspector]
- public float gravity = -20f;
- [HideInInspector]
- public bool useGravity = true;
- public RayCollisions2D detections;
- protected Collider2D col;
- [Header("Debug")]
- public Color lineColor;
- protected Vector2 vel;
- public Vector2 velocity { get => vel; set => vel = value; }
- protected virtual void Awake()
- {
- col = GetComponent<BoxCollider2D>();
- detections = new RayCollisions2D(col);
- detections.horizontalRayCount = detections.verticalRayCount = 4;
- detections.CalculateRaySpacing();
- }
- public virtual void Move(Vector2 input)
- {
- if (detections.collision.above || detections.collision.below) vel.y = 0;
- vel.x = input.x;
- vel.y += gravity * Time.deltaTime;
- Vector2 value = vel * Time.deltaTime;
- detections.UpdateRaycastOrigins();
- detections.collision.Reset();
- if (value.y != 0)
- VerticalCollisions(ref value);
- if (value.x != 0)
- HorizontalCollisions(ref value);
- transform.Translate(value);
- }
- protected virtual void VerticalCollisions(ref Vector2 velocity)
- {
- float directionY = Mathf.Sign(velocity.y);
- float rayLength = Mathf.Abs(velocity.y) + RayCollisions2D.skinWidth;
- for (int i = 0; i < detections.verticalRayCount; i++)
- {
- Vector2 rayOrigin = (directionY == -1) ? detections.collisionOrigin.bottomLeft : detections.collisionOrigin.topLeft;
- rayOrigin += Vector2.right * (detections.verticalRaySpacing * i + velocity.x);
- RaycastHit2D[] hits = Physics2D.RaycastAll(rayOrigin, Vector2.up * directionY, rayLength);
- Debug.DrawRay(rayOrigin, Vector2.up * directionY * rayLength, lineColor);
- for (int h = 0; h < hits.Length; h++)
- {
- RaycastHit2D hit = hits[h];
- if (hit && hit.transform.gameObject != gameObject)
- {
- velocity.y = (hit.distance - RayCollisions2D.skinWidth) * directionY;
- rayLength = hit.distance;
- detections.collision.below = directionY == -1;
- detections.collision.above = directionY == 1;
- break;
- }
- }
- }
- }
- protected virtual void HorizontalCollisions(ref Vector2 velocity)
- {
- float directionX = Mathf.Sign(velocity.x);
- float rayLength = Mathf.Abs(velocity.x) + RayCollisions2D.skinWidth;
- for (int i = 0; i < detections.horizontalRayCount; i++)
- {
- Vector2 rayOrigin = (directionX == -1) ? detections.collisionOrigin.bottomLeft : detections.collisionOrigin.bottomRight;
- rayOrigin += Vector2.up * (detections.horizontalRaySpacing * i);
- RaycastHit2D[] hits = Physics2D.RaycastAll(rayOrigin, Vector2.right * directionX, rayLength);
- Debug.DrawRay(rayOrigin, Vector2.right * directionX * rayLength, lineColor);
- for (int h = 0; h < hits.Length; h++)
- {
- RaycastHit2D hit = hits[h];
- if (hit && hit.transform.gameObject != gameObject)
- {
- velocity.x = (hit.distance - RayCollisions2D.skinWidth) * directionX;
- rayLength = hit.distance;
- detections.collision.left = directionX == -1;
- detections.collision.right = directionX == 1;
- break;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement