So, I'm developing this small 2D project where I have a sphere that detects if objects get close to it from three different directions.
I have successfully found a way to detect if an object touches my line if the object is referenced to a certain LayerMask
, in this case the walls.
So, basically I'm doing this:
var layerMask = 1 << 10; //wall layer
float left = 0f, middle = 0f, right = 0f;
Debug.DrawLine (leftStart.position, leftEnd.position, Color.green);
Debug.DrawLine (middleStart.position, middleEnd.position, Color.green);
Debug.DrawLine (rightStart.position, rightEnd.position, Color.green);
leftSpot = Physics2D.Linecast(leftStart.position, leftEnd.position, layerMask);
middleSpot = Physics2D.Linecast(middleStart.position, middleEnd.position, layerMask);
rightSpot = Physics2D.Linecast(rightStart.position, rightEnd.position, layerMask);
Now, what I would like to do is reference the right wall (Horizontal or vertical) and calculate the distance between the wall and my sphere with:
Vector2.Distance(transform.position, wall1.position);
How can I get the reference to the correct wall to do this?
Thank you
public
variable. I am just using its layermask because I would like the same behavior for every wall \$\endgroup\$