Advertisement
Ryoh

ProjectileController w/ Destructible tile reference

Mar 4th, 2020
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.46 KB | None | 0 0
  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.     //For some reason I couldn't set this with the inspector
  12.     //I think its a bug; So try setting this in the inspector with [SerializeField]
  13.     DestructibleTiles tiles;
  14.  
  15.     public override void Start()
  16.     {
  17.         //If you can't set in the inspector this will take care of it.
  18.         if(tiles == null)
  19.         {
  20.             //I don't like FindObjectOfType, It is very slow.
  21.             //The only reason I use it here is I am pooling my fireballs; So I only have to do it on load
  22.             //I don't recommend this for instantiating
  23.             tiles = FindObjectOfType<DestructibleTiles>();
  24.         }
  25.  
  26.         base.Start();
  27.     }
  28.  
  29.     public void Move(Vector3 velocity)
  30.     {
  31.         UpdateRayCastOrigins();
  32.         collisions.Reset();
  33.  
  34.         if (velocity.x != 0)
  35.         {
  36.             HorizontalCollisions(ref velocity);
  37.         }
  38.         if (velocity.y != 0)
  39.         {
  40.             VerticalCollisions(ref velocity);
  41.         }
  42.  
  43.         transform.Translate(velocity);
  44.     }
  45.  
  46.     void HorizontalCollisions(ref Vector3 velocity)
  47.     {
  48.         float directionX = Mathf.Sign(velocity.x);
  49.         float rayLength = Mathf.Abs(velocity.x) + skinWidth;
  50.  
  51.         for (int i = 0; i < horizontalRayCount; i++)
  52.         {
  53.             Vector2 rayOrigin = (directionX == -1) ? raycastOrigins.bottomLeft : raycastOrigins.bottomRight;
  54.             rayOrigin += Vector2.up * (horizontalRaySpacing * i);
  55.             RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.right * directionX, rayLength, collisionMask);
  56.  
  57.             Debug.DrawRay(rayOrigin, Vector2.right * directionX * rayLength, Color.red);
  58.  
  59.             if (hit)
  60.             {
  61.                 //Pass hit information to tiles
  62.                 tiles.DestroyTileAtPosition(hit);
  63.                 velocity.x = (hit.distance - skinWidth) * directionX;
  64.                 rayLength = hit.distance;
  65.  
  66.                 collisions.left = directionX == -1;
  67.                 collisions.right = directionX == 1;
  68.             }
  69.         }
  70.     }
  71.  
  72.     void VerticalCollisions(ref Vector3 velocity)
  73.     {
  74.         float directionY = Mathf.Sign(velocity.y);
  75.         float rayLength = Mathf.Abs(velocity.y) + skinWidth;
  76.  
  77.         for (int i = 0; i < verticalRayCount; i++)
  78.         {
  79.             Vector2 rayOrigin = (directionY == -1) ? raycastOrigins.bottomLeft : raycastOrigins.topLeft;
  80.             rayOrigin += Vector2.right * (verticalRaySpacing * i + velocity.x);
  81.             RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.up * directionY, rayLength, collisionMask);
  82.  
  83.             Debug.DrawRay(rayOrigin, Vector2.up * directionY * rayLength, Color.red);
  84.  
  85.             if (hit)
  86.             {
  87.                 //Pass hit information to tiles
  88.                 tiles.DestroyTileAtPosition(hit);
  89.                 velocity.y = (hit.distance - skinWidth) * directionY;
  90.                 rayLength = hit.distance;
  91.  
  92.                 collisions.below = directionY == -1;
  93.                 collisions.above = directionY == 1;
  94.             }
  95.         }
  96.     }
  97.  
  98.     public struct CollisionInfo
  99.     {
  100.         public bool above, below;
  101.         public bool left, right;
  102.  
  103.         public void Reset()
  104.         {
  105.             above = below = false;
  106.             left = right = false;
  107.         }
  108.     }
  109.  
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement