- Home /
Detect if all objects between two points are of same tag in Unity
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;
}
Answer by jebemti · Mar 17, 2016 at 10:34 PM
Use Physics.RaycastAll to get all objects in a direction.
Iterate over the array and check for the objects' tags.
Your answer
Follow this Question
Related Questions
the "blocking object" is not work 0 Answers
Unity raycast only sometimes works 0 Answers
How to get UI element into HMD Unity 5.3.4p1 - Oculus Utilities 1.3 0 Answers
Change Transparency 1 Answer
Need help with OnUse() script! 1 Answer