-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Push JKump
- Loading branch information
Showing
5 changed files
with
662 additions
and
356 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
using UnityEngine; | ||
using System.Collections; | ||
|
||
[RequireComponent(typeof(BoxCollider2D))] //Require BoxCollider | ||
public class Controller2D : MonoBehaviour | ||
{ | ||
public LayerMask collisionMask; //Objects we collide with | ||
|
||
const float skinWidth = .25f; //const so you cant change the value, and width | ||
public int horizontalRayCount = 4; //Amount of rays shooting horizontally | ||
public int verticalRayCount = 4;//Amount of rays shooting Vertically | ||
|
||
float horizontalRaySpacing; //Spacing between each horizontal ray | ||
float verticalRaySpacing; //Spacing between each vertical ray | ||
|
||
BoxCollider2D collider; //Reference | ||
RaycastOrigins raycastOrigins; //Reference to raycastorigins | ||
public CollisionInfo collisions; //Reference to Collison Info | ||
|
||
void Start() | ||
{ | ||
collider = GetComponent<BoxCollider2D>(); | ||
CalculateRaySpacing(); //never changes | ||
} | ||
|
||
public void Move(Vector3 velocity) //Actually Moving | ||
{ | ||
UpdateRaycastOrigins(); //Everytime we move | ||
collisions.Reset(); //Blank Slate | ||
|
||
if (velocity.x != 0)//Horizontal Axis | ||
{ | ||
HorizontalCollisions(ref velocity); | ||
} | ||
if (velocity.y != 0) //Vertical Axis | ||
{ | ||
VerticalCollisions(ref velocity); | ||
} | ||
|
||
transform.Translate(velocity); //Actually modifies the velocity | ||
} | ||
|
||
void HorizontalCollisions(ref Vector3 velocity) //reference to velocity vector (dont make a copy) | ||
{ | ||
float directionX = Mathf.Sign(velocity.x); //depending on moving left or right | ||
float rayLength = Mathf.Abs(velocity.x) + skinWidth; //length of the ray (always positve) | ||
|
||
for (int i = 0; i < horizontalRayCount; i++) | ||
{ | ||
Vector2 rayOrigin = (directionX == -1) ? raycastOrigins.bottomLeft : raycastOrigins.bottomRight;// See if moving up or down, then choose corresponding | ||
rayOrigin += Vector2.up * (horizontalRaySpacing * i);//cast rays from where we will be once moving | ||
RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.right * directionX, rayLength, collisionMask);//raycast from origin and have it go up, length of the ray, layer mask | ||
|
||
Debug.DrawRay(rayOrigin, Vector2.right * directionX * rayLength, Color.red);//multiply to maintain direction | ||
|
||
if (hit) | ||
{ | ||
velocity.x = (hit.distance - skinWidth) * directionX; //Multiply to maintain direction | ||
rayLength = hit.distance; //length of ray is now the distance to hit | ||
|
||
collisions.left = directionX == -1; //if hit and going left, collison left is true | ||
collisions.right = directionX == 1; //if hit and going right, collision right is true | ||
} | ||
} | ||
} | ||
|
||
void VerticalCollisions(ref Vector3 velocity) //reference to velocity vector (dont make a copy) | ||
{ | ||
float directionY = Mathf.Sign(velocity.y); //depending on moving up or done | ||
float rayLength = Mathf.Abs(velocity.y) + skinWidth; //length of the ray (always positve) | ||
|
||
for (int i = 0; i < verticalRayCount; i++) | ||
{ | ||
Vector2 rayOrigin = (directionY == -1) ? raycastOrigins.bottomLeft : raycastOrigins.topLeft; // See if moving up or down, then choose corresponding | ||
rayOrigin += Vector2.right * (verticalRaySpacing * i + velocity.x); //cast rays from where we will be once moving | ||
RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.up * directionY, rayLength, collisionMask); //raycast from origin and have it go up, length of the ray, layer mask | ||
|
||
Debug.DrawRay(rayOrigin, Vector2.up * directionY * rayLength, Color.red); | ||
|
||
if (hit) //if it hits | ||
{ | ||
velocity.y = (hit.distance - skinWidth) * directionY; //multiply to maintain direction | ||
rayLength = hit.distance; //length of ray is now the distance to hit | ||
collisions.below = directionY == -1; //if hit and going down, below is true | ||
collisions.above = directionY == 1; //if hit and going up, above is true | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
void UpdateRaycastOrigins() //Update raycast origins | ||
{ | ||
Bounds bounds = collider.bounds; //Getting bounds from the collider | ||
bounds.Expand(skinWidth * -2); //Shrinks | ||
|
||
raycastOrigins.bottomLeft = new Vector2(bounds.min.x, bounds.min.y); //Bottom Left corner | ||
raycastOrigins.bottomRight = new Vector2(bounds.max.x, bounds.min.y);//Bottom Right corner | ||
raycastOrigins.topLeft = new Vector2(bounds.min.x, bounds.max.y); //Top Left corner | ||
raycastOrigins.topRight = new Vector2(bounds.max.x, bounds.max.y);//Top Right corner | ||
} | ||
|
||
void CalculateRaySpacing() //Calculates space between rays | ||
{ | ||
Bounds bounds = collider.bounds; //set bounds | ||
bounds.Expand(skinWidth * -2); //Shrink | ||
|
||
horizontalRayCount = Mathf.Clamp(horizontalRayCount, 2, int.MaxValue); //Need 1 in each corner | ||
verticalRayCount = Mathf.Clamp(verticalRayCount, 2, int.MaxValue); //1 in each corner (vertical) | ||
|
||
horizontalRaySpacing = bounds.size.y / (horizontalRayCount - 1); //The size of bounds / n - 1 | ||
verticalRaySpacing = bounds.size.x / (verticalRayCount - 1);//The size of bounds / n - 1 | ||
} | ||
|
||
struct RaycastOrigins //Holds Vector 2's | ||
{ | ||
public Vector2 topLeft, topRight; | ||
public Vector2 bottomLeft, bottomRight; | ||
} | ||
|
||
public struct CollisionInfo | ||
{ | ||
public bool above, below; | ||
public bool left, right; | ||
|
||
public void Reset() //Reset the Bools | ||
{ | ||
above = below = false; | ||
left = right = false; | ||
} | ||
} | ||
|
||
} | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.