Skip to main content
added 8 characters in body
Source Link
House
  • 73.5k
  • 17
  • 188
  • 276

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(Vector3leftStart.leftposition, Vector3leftEnd.left*2position, hitResults, 1layerMask);
    
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.

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(Vector3.left, Vector3.left*2, hitResults, 1);
    
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.

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.

deleted 504 characters in body
Source Link
House
  • 73.5k
  • 17
  • 188
  • 276

There is another version of LineCast youYou should be using:

public static bool Linecast(Vector3 start, Vector3 end, RaycastHit hitInfo, int layerMask = DefaultRaycastLayers); Description

Returns true if there is any collider intersecting the line between start and end.

If true is returned, hitInfo will contain more information about where the collider was hit (See Also: RaycastHit). Layer mask is used to selectively ignore colliders when casting a ray.

This version has a RaycastHit object to be passed in as well. If there isgetting a collision, this hitInfo object will be populated with information about whichRaycastHit2D object was hitback from your test. Even better, one of the properties contained in the RaycastHit object isYou can access the distancehit info from the origin of the raythat to the intersection point.

Also note,get the documentation doesn't show it, but you may have to pass indistance and other information about the RaycastHit object with the out parameter, somethingyou hit. Something like the following:

RaycastHitRaycastHit2D leftHitInfo;

leftSpotleftHitInfo = PhysicsPhysics2D.Linecast(leftStart.position,
           leftEnd.position, outlayerMask);

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(Vector3.left, layerMaskVector3.left*2, hitResults, 1);
    
if(leftSpothitCount > 0) {
    Debug.Log("Left hit! DistanceFirst distance: " + leftHitInfohitResults[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.

There is another version of LineCast you should be using:

public static bool Linecast(Vector3 start, Vector3 end, RaycastHit hitInfo, int layerMask = DefaultRaycastLayers); Description

Returns true if there is any collider intersecting the line between start and end.

If true is returned, hitInfo will contain more information about where the collider was hit (See Also: RaycastHit). Layer mask is used to selectively ignore colliders when casting a ray.

This version has a RaycastHit object to be passed in as well. If there is a collision, this hitInfo object will be populated with information about which object was hit. Even better, one of the properties contained in the RaycastHit object is the distance from the origin of the ray to the intersection point.

Also note, the documentation doesn't show it, but you may have to pass in the RaycastHit object with the out parameter, something like the following:

RaycastHit leftHitInfo;

leftSpot = Physics.Linecast(leftStart.position,
           leftEnd.position, out leftHitInfo, layerMask);

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

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(Vector3.left, Vector3.left*2, hitResults, 1);
    
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.

deleted 2 characters in body
Source Link
House
  • 73.5k
  • 17
  • 188
  • 276

There is another version of LineCast you should be using:

public static bool Linecast(Vector3 start, Vector3 end, RaycastHit hitInfo, int layerMask = DefaultRaycastLayers); Description

Returns true if there is any collider intersecting the line between start and end.

If true is returned, hitInfo will contain more information about where the collider was hit (See Also: RaycastHit). Layer mask is used to selectively ignore colliders when casting a ray.

This version has a RaycastHit object to be passed in as well. If there is a collision, this hitInfo object will be populated with information about which object was hit. Even better, one of the properties contained in the RaycastHit object is the distance from the origin of the ray to the intersection point.

SomethingAlso note, the documentation doesn't show it, but you may have to pass in the RaycastHit object with the out parameter, something like the following:

RaycastHit leftHitInfo = new RaycastHit();leftHitInfo;

leftSpot = Physics2DPhysics.Linecast(leftStart.position,
           leftEnd.position, out leftHitInfo, layerMask);

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

There is another version of LineCast you should be using:

public static bool Linecast(Vector3 start, Vector3 end, RaycastHit hitInfo, int layerMask = DefaultRaycastLayers); Description

Returns true if there is any collider intersecting the line between start and end.

If true is returned, hitInfo will contain more information about where the collider was hit (See Also: RaycastHit). Layer mask is used to selectively ignore colliders when casting a ray.

This version has a RaycastHit object to be passed in as well. If there is a collision, this hitInfo object will be populated with information about which object was hit. Even better, one of the properties contained in the RaycastHit object is the distance from the origin of the ray to the intersection point.

Something like the following:

RaycastHit leftHitInfo = new RaycastHit();

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

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

There is another version of LineCast you should be using:

public static bool Linecast(Vector3 start, Vector3 end, RaycastHit hitInfo, int layerMask = DefaultRaycastLayers); Description

Returns true if there is any collider intersecting the line between start and end.

If true is returned, hitInfo will contain more information about where the collider was hit (See Also: RaycastHit). Layer mask is used to selectively ignore colliders when casting a ray.

This version has a RaycastHit object to be passed in as well. If there is a collision, this hitInfo object will be populated with information about which object was hit. Even better, one of the properties contained in the RaycastHit object is the distance from the origin of the ray to the intersection point.

Also note, the documentation doesn't show it, but you may have to pass in the RaycastHit object with the out parameter, something like the following:

RaycastHit leftHitInfo;

leftSpot = Physics.Linecast(leftStart.position,
           leftEnd.position, out leftHitInfo, layerMask);

if(leftSpot) {
   Debug.Log("Left hit! Distance: " + leftHitInfo.distance.ToString());
}
deleted 109 characters in body
Source Link
House
  • 73.5k
  • 17
  • 188
  • 276
Loading
Source Link
House
  • 73.5k
  • 17
  • 188
  • 276
Loading