SHARE
TWEET

EntityController

a guest Dec 12th, 2019 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. [RequireComponent(typeof(BoxCollider2D))]
  7. public class EntityController : MonoBehaviour
  8. {
  9.     // Start is called before the first frame update
  10.  
  11.  
  12.     [HideInInspector]
  13.     public float gravity = -20f;
  14.     [HideInInspector]
  15.     public bool useGravity = true;
  16.     public RayCollisions2D detections;
  17.  
  18.     protected Collider2D col;
  19.  
  20.     [Header("Debug")]
  21.     public Color lineColor;
  22.  
  23.  
  24.     protected Vector2 vel;
  25.  
  26.     public Vector2 velocity { get => vel; set => vel = value; }
  27.     protected virtual void Awake()
  28.     {
  29.  
  30.         col = GetComponent<BoxCollider2D>();
  31.         detections = new RayCollisions2D(col);
  32.         detections.horizontalRayCount = detections.verticalRayCount = 4;
  33.         detections.CalculateRaySpacing();
  34.  
  35.     }
  36.  
  37.  
  38.     public virtual void Move(Vector2 input)
  39.     {
  40.  
  41.  
  42.  
  43.         if (detections.collision.above || detections.collision.below) vel.y = 0;
  44.  
  45.  
  46.         vel.x = input.x;
  47.         vel.y += gravity * Time.deltaTime;
  48.  
  49.  
  50.  
  51.  
  52.         Vector2 value = vel * Time.deltaTime;
  53.  
  54.         detections.UpdateRaycastOrigins();
  55.         detections.collision.Reset();
  56.  
  57.         if (value.y != 0)
  58.             VerticalCollisions(ref value);
  59.         if (value.x != 0)
  60.             HorizontalCollisions(ref value);
  61.         transform.Translate(value);
  62.  
  63.  
  64.     }
  65.  
  66.  
  67.  
  68.     protected virtual void VerticalCollisions(ref Vector2 velocity)
  69.     {
  70.         float directionY = Mathf.Sign(velocity.y);
  71.         float rayLength = Mathf.Abs(velocity.y) + RayCollisions2D.skinWidth;
  72.         for (int i = 0; i < detections.verticalRayCount; i++)
  73.         {
  74.             Vector2 rayOrigin = (directionY == -1) ? detections.collisionOrigin.bottomLeft : detections.collisionOrigin.topLeft;
  75.             rayOrigin += Vector2.right * (detections.verticalRaySpacing * i + velocity.x);
  76.             RaycastHit2D[] hits = Physics2D.RaycastAll(rayOrigin, Vector2.up * directionY, rayLength);
  77.             Debug.DrawRay(rayOrigin, Vector2.up * directionY * rayLength, lineColor);
  78.             for (int h = 0; h < hits.Length; h++)
  79.             {
  80.                 RaycastHit2D hit = hits[h];
  81.                 if (hit && hit.transform.gameObject != gameObject)
  82.                 {
  83.  
  84.                     velocity.y = (hit.distance - RayCollisions2D.skinWidth) * directionY;
  85.                     rayLength = hit.distance;
  86.  
  87.                     detections.collision.below = directionY == -1;
  88.                     detections.collision.above = directionY == 1;
  89.                     break;
  90.                 }
  91.             }
  92.  
  93.  
  94.         }
  95.     }
  96.  
  97.  
  98.  
  99.     protected virtual void HorizontalCollisions(ref Vector2 velocity)
  100.     {
  101.         float directionX = Mathf.Sign(velocity.x);
  102.         float rayLength = Mathf.Abs(velocity.x) + RayCollisions2D.skinWidth;
  103.         for (int i = 0; i < detections.horizontalRayCount; i++)
  104.         {
  105.             Vector2 rayOrigin = (directionX == -1) ? detections.collisionOrigin.bottomLeft : detections.collisionOrigin.bottomRight;
  106.             rayOrigin += Vector2.up * (detections.horizontalRaySpacing * i);
  107.             RaycastHit2D[] hits = Physics2D.RaycastAll(rayOrigin, Vector2.right * directionX, rayLength);
  108.             Debug.DrawRay(rayOrigin, Vector2.right * directionX * rayLength, lineColor);
  109.             for (int h = 0; h < hits.Length; h++)
  110.             {
  111.                 RaycastHit2D hit = hits[h];
  112.                 if (hit && hit.transform.gameObject != gameObject)
  113.                 {
  114.                     velocity.x = (hit.distance - RayCollisions2D.skinWidth) * directionX;
  115.                     rayLength = hit.distance;
  116.  
  117.                     detections.collision.left = directionX == -1;
  118.                     detections.collision.right = directionX == 1;
  119.                     break;
  120.                 }
  121.             }
  122.  
  123.         }
  124.     }
  125.  
  126. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top