0
\$\begingroup\$

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.

enter image description here

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

\$\endgroup\$
4
  • \$\begingroup\$ Is the wall you're referencing coming into contact with the LineCast? If so, you'll get a reference to the object back when there's a collision. \$\endgroup\$
    – House
    Commented Feb 15, 2015 at 18:29
  • \$\begingroup\$ I am not passing it to the class as a public variable. I am just using its layermask because I would like the same behavior for every wall \$\endgroup\$ Commented Feb 15, 2015 at 18:31
  • \$\begingroup\$ I'm sorry that doesn't make a lot of sense to me. Is the wall you want to get the distance to within the distance of the LineCast? Or are you wanting to get a reference to the nearest wall? \$\endgroup\$
    – House
    Commented Feb 15, 2015 at 18:41
  • \$\begingroup\$ Don't worry, I am completely new to this. So, I might be doing things in the wrong way. leftSpot, middleSpot, and rightSpot become true whenever the left, middle, or right line touch a wall. I want to get a referenced to that touched wall and calculate the distance between that wall and the sphere \$\endgroup\$ Commented Feb 15, 2015 at 18:43

1 Answer 1

1
\$\begingroup\$

You should be getting a RaycastHit2D object back from your test. You can access the hit info from that to get the distance and other information about the object you hit. Something like the following:

RaycastHit2D leftHitInfo = Physics2D.Linecast(leftStart.position,
           leftEnd.position, layerMask);

if(leftHitInfo.collider != null) {
   Debug.Log("Left hit! Distance: " + leftHitInfo.distance.ToString());
}

If you're performing this test regularly, I suggest you use the non-alloc version of this method:

int maxReturnedIntersections = 5;
RaycastHit2D[] hitResults = new RaycastHit2D[maxReturnedIntersections];

int hitCount = Physics2D.LinecastNonAlloc(leftStart.position, leftEnd.position, hitResults, layerMask);

if(hitCount > 0) {
    Debug.Log("Left hit! First distance: " + hitResults[0].distance.ToString());
}

Ideally, you'd create the array of hit results outside of your method so you're not creating a new one over and over.

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .