SHARE
TWEET

ProjectileController

Ryoh Feb 14th, 2020 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5.  
  6. [RequireComponent(typeof(BoxCollider2D))]
  7. public class ProjectileController2D : RaycastController
  8. {
  9.     public CollisionInfo collisions;
  10.  
  11.     public override void Start()
  12.     {
  13.         base.Start();
  14.     }
  15.  
  16.     public void Move(Vector3 velocity)
  17.     {
  18.         UpdateRayCastOrigins();
  19.         collisions.Reset();
  20.  
  21.         if (velocity.x != 0)
  22.         {
  23.             HorizontalCollisions(ref velocity);
  24.         }
  25.         if (velocity.y != 0)
  26.         {
  27.             VerticalCollisions(ref velocity);
  28.         }
  29.  
  30.         transform.Translate(velocity);
  31.     }
  32.  
  33.     void HorizontalCollisions(ref Vector3 velocity)
  34.     {
  35.         float directionX = Mathf.Sign(velocity.x);
  36.         float rayLength = Mathf.Abs(velocity.x) + skinWidth;
  37.  
  38.         for (int i = 0; i < horizontalRayCount; i++)
  39.         {
  40.             Vector2 rayOrigin = (directionX == -1) ? raycastOrigins.bottomLeft : raycastOrigins.bottomRight;
  41.             rayOrigin += Vector2.up * (horizontalRaySpacing * i);
  42.             RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.right * directionX, rayLength, collisionMask);
  43.  
  44.             Debug.DrawRay(rayOrigin, Vector2.right * directionX * rayLength, Color.red);
  45.  
  46.             if (hit)
  47.             {
  48.                 velocity.x = (hit.distance - skinWidth) * directionX;
  49.                 rayLength = hit.distance;
  50.  
  51.                 collisions.left = directionX == -1;
  52.                 collisions.right = directionX == 1;
  53.             }
  54.         }
  55.     }
  56.  
  57.     void VerticalCollisions(ref Vector3 velocity)
  58.     {
  59.         float directionY = Mathf.Sign(velocity.y);
  60.         float rayLength = Mathf.Abs(velocity.y) + skinWidth;
  61.  
  62.         for (int i = 0; i < verticalRayCount; i++)
  63.         {
  64.             Vector2 rayOrigin = (directionY == -1) ? raycastOrigins.bottomLeft : raycastOrigins.topLeft;
  65.             rayOrigin += Vector2.right * (verticalRaySpacing * i + velocity.x);
  66.             RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.up * directionY, rayLength, collisionMask);
  67.  
  68.             Debug.DrawRay(rayOrigin, Vector2.up * directionY * rayLength, Color.red);
  69.  
  70.             if (hit)
  71.             {
  72.                 velocity.y = (hit.distance - skinWidth) * directionY;
  73.                 rayLength = hit.distance;
  74.  
  75.                 collisions.below = directionY == -1;
  76.                 collisions.above = directionY == 1;
  77.             }
  78.         }
  79.     }
  80.  
  81.     public struct CollisionInfo
  82.     {
  83.         public bool above, below;
  84.         public bool left, right;
  85.  
  86.         public void Reset()
  87.         {
  88.             above = below = false;
  89.             left = right = false;
  90.         }
  91.     }
  92.  
  93. }
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