Created
December 17, 2013 04:00
-
-
Save AlexBezuska/7999860 to your computer and use it in GitHub Desktop.
Experiment in casting boxes as rays for unity in javascript. Collision Detection Game Dev
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
var tName; | |
// center | |
public var center : Vector2; | |
var centerX : float; | |
var centerY : float; | |
var height : float; | |
var halfHeight : float; | |
var width : float; | |
var rayLength : float = .1; | |
var rayWidth : float = .01; | |
var left : float; | |
var right : float; | |
var top : float; | |
var bottom : float; | |
var hitBoxHit : boolean; | |
var topLeftHit : boolean = false; | |
var topRightHit : boolean = false; | |
var rightTopHit : boolean = false; | |
var rightBottomHit : boolean = false; | |
var bottomRightHit : boolean = false; | |
var bottomLeftHit : boolean = false; | |
var leftBottomHit : boolean = false; | |
var leftTopHit : boolean = false; | |
var player : GameObject; | |
var Vert : Vector2; | |
var Hori : Vector2; | |
function Start () { | |
Vert = Vector2.up; | |
Hori = Vector2.right; | |
} | |
function drawBox(width: float, height: float, top: float, right: float, bottom: float, left: float, color){ | |
Debug.DrawRay (Vector2(left , bottom), Hori * width, color); // top line | |
Debug.DrawRay (Vector2(right , top), Vert * height, color); // right line | |
Debug.DrawRay (Vector2(left , top), Hori * width, color); //bottom line | |
Debug.DrawRay (Vector2(left , top), Vert * height, color); // right line | |
} | |
function hitBox(other : GameObject, top : float, right : float, bottom : float, left : float, width : float, height : float, color){ | |
var otherCenter : Vector2 = other.transform.renderer.bounds.center; | |
var otherCenteright : float = otherCenter.x; | |
var otherCenterY : float = otherCenter.y; | |
var otherHeight : float = other.transform.renderer.bounds.size.y; | |
var otherHalfHeight : float = otherHeight/2; | |
var otherWidth : float = other.transform.renderer.bounds.size.x; | |
var otherHalfWidth: float = otherWidth/2; | |
var otherLeft: float = otherCenteright - otherHalfWidth; | |
var otherRight: float = otherCenteright + otherHalfWidth; | |
var otherTop: float = otherCenterY - otherHalfHeight; | |
var otherBottom: float = otherCenterY + otherHalfHeight; | |
drawBox( otherWidth, otherHeight, otherTop, otherRight, otherBottom, otherLeft, Color.magenta); | |
if (otherLeft < right && otherRight > left && | |
otherTop < bottom && otherBottom > top) { | |
// The objects are touching | |
drawBox(width, height, top, right, bottom, left, Color.red); | |
return true; | |
}else{ | |
drawBox(width, height, top, right, bottom, left, color); | |
return false; | |
} | |
}// hitBox | |
function Update () { | |
// Update all coords of this object | |
center = transform.renderer.bounds.center; | |
centerX = center.x; | |
centerY = center.y; | |
tName = transform.name; | |
height = transform.renderer.bounds.size.y; | |
halfHeight = height/2; | |
width = transform.renderer.bounds.size.x; | |
halfWidth = width/2; | |
left = centerX - halfWidth; | |
right = centerX + halfWidth; | |
top = centerY - halfHeight; | |
bottom = centerY + halfHeight; | |
// test for all hits | |
hitBoxHit = hitBox(player, top, right, bottom, left, width, height, Color.green); | |
topLeftHit = hitBox(player, bottom, left+rayWidth , bottom+rayLength, left, rayWidth, rayLength, Color.white); | |
topRightHit = hitBox(player, bottom, right, bottom+rayLength, right-rayWidth, rayWidth, rayLength, Color.white); | |
rightTopHit = hitBox(player, bottom-rayWidth, right+rayLength, bottom, right, rayLength, rayWidth, Color.white); | |
rightBottomHit = hitBox(player, top , left , top+rayWidth , left-rayLength, rayLength, rayWidth, Color.white); | |
bottomLeftHit = hitBox(player, top-rayLength, left+rayWidth, top, left, rayWidth, rayLength, Color.white); | |
bottomRightHit = hitBox(player, top-rayLength, right , top , right-rayWidth, rayWidth, rayLength, Color.white); | |
leftTopHit = hitBox(player, bottom-rayWidth, left , bottom, left-rayLength, rayLength, rayWidth, Color.white); | |
leftBottomHit = hitBox(player, top , right+rayLength, top+rayWidth, right , rayLength, rayWidth, Color.white); | |
}// end update | |
// Ray Syntax : Debug.DrawRay (Vector2(x , y), size, color); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment