I am making a game where the floor consists of individual live and dead pixels. If a line of live pixels are drawn between the two points, you pass. If the line is broken, you cannot pass.
I would like to detect wether all objects between two points are of the same Tag. Here is a drawing to try and illustrate this:
At the moment, I have the following code that checks if the next “pixel” is live or dead using RayCasts:
function Update () {
var pixfwd = transform.TransformDirection (Vector3.up * Reach);
var pixbwd = transform.TransformDirection (Vector3.down * Reach);
var pixleft = transform.TransformDirection (Vector3.left * Reach);
var pixright = transform.TransformDirection (Vector3.right * Reach);
Debug.DrawRay(transform.position, pixfwd * Reach, Color.red, 0.1f);
Debug.DrawRay(transform.position, pixbwd * Reach, Color.green, 0.1f);
Debug.DrawRay(transform.position, pixleft * Reach, Color.yellow, 0.1f);
Debug.DrawRay(transform.position, pixright * Reach, Color.blue, 0.1f);
Physics.Raycast (transform.position, pixfwd, pixhit);
Physics.Raycast (transform.position, pixbwd, pixhit2);
Physics.Raycast (transform.position, pixleft, pixhit3);
Physics.Raycast (transform.position, pixright, pixhit4);
if ( checkVision(pixhit) || checkVision(pixhit2) || checkVision(pixhit3) || checkVision(pixhit4) ) {
nextisLive = true;
}
else
{
nextisLive=false;
}
}
function checkVision(pixhit:RaycastHit):boolean
{
if ( pixhit != null && pixhit.collider != null && pixhit.collider.tag == "Live" )
{
return true;
}
return false;
if ( pixhit2 != null && pixhit2.collider != null && pixhit2.collider.tag == "Live" )
{
return true;
}
return false;
if ( pixhit3 != null && pixhit3.collider != null && pixhit3.collider.tag == "Live" )
{
return true;
}
return false;
if ( pixhit4 != null && pixhit4.collider != null && pixhit4.collider.tag == "Live" )
{
return true;
}
return false;
}