Vector2.Distance doesn't return same result as Raycasthit2D?

I’m gathering the Vector2.Distance from ObjectA to ObjectB, then casting a ray2d from ObjectA to ObjectB using that distance to determine if anything is in the way. What’s unusual is that not only is the ray hitting an object(which does not exist) but it’s hitting an object prior to the actual distance between ObjectA and ObjectB.
Below is the code I’m using to better show the situation.

Getting the location to move/cast to

  //manually set the mouse position to a vector3
  Vector3 mousePosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0);
  //set world destination)
  Vector3 worldPosition = Camera.main.ScreenToWorldPoint(mousePosition);
  Vector2 flatWorldPosition = new Vector2(worldPosition.x, worldPosition.y);
  RoundVector2ToGrid(ref flatWorldPosition);
  //after rounding to nearest tile set move destination.
  _move.Destination = flatWorldPosition;
  //find the best way to reach this destination
  FindPath(flatWorldPosition);

Checking for collision using raycast

private void FindPath(Vector2 destination)
    {
//this is a test object without a collider that is moved to my Vector2 destination.
//it appears to be placed properly. used for testing
  GameObject testObj = GameObject.FindGameObjectWithTag("Finish");
  testObj.transform.position = destination;

        RaycastHit2D hit;
        //get current position of object
        Vector2 objectPosition = new Vector2(transform.position.x, transform.position.y);
        //starting position of ray and max cast distance
        Vector2 rayStart;
        float castDistance;
        float quickestPath = -1f;

        //first check if the object can reach its destination without any way points
        rayStart = objectPosition;
        //distance between obj and final dest
        castDistance = Vector2.Distance(rayStart, destination);
        //fire ray
        hit = Physics2D.Raycast(rayStart, destination, castDistance);
        //if we dont hit anything then its a clean shot to the final destination.
        //set move location and get out of method
        if (hit.distance == 0)
        {
      //for some reason hit.distance is always > 0 even with no obstacles in the way.
//hit.distance is < than castDistance

Don’t suppose I’m missing something here?

  1. FindGameObejctWithTag is slow, you should cache that into a field/property somewhere, so you’re not constantly calling that function.

  2. The RaycastHit2D that comes back from Physics2D.Raycast should have a reference to the collider, transform, and rigidbody of whatever it hit… check what it hit. Just print it out or something.

  1. is just for testing to see where I’m clicking.

  2. I tried this but I was just getting the name of the component eg: Collider2D. What method would I use to get the GameObjects name that the collider belongs to?

hit.collider.gameObject.name

Also, the cache would be super simple:

private GameObject _testObj;
private void FindPath(Vector2 destination)
{
    if(_testObj == null) _testObj = GameObject.FindGameObjectWithTag("Finish");
...

Its because hit distance is the distance to the point in space where you collide with a given collider. Not the collider objects position which you are using in your distance check

I believe I found the problem, but I’m not sure how to fix it.

On line 22 I’m casting to my destination, which is where I want my object to move to. However, the ray is not casting in that direction, it seems to be casting in a completely different direction. So I’m assuming that the ‘direction’ variable in raycast isn’t the literal sense of position of where you wish the ray to go but rather an angle.

For example, let’s say I want to move to 65, -65 – and let’s pretend my character is at 75, -75. My characters pathing would be from SE moving to NE, but some reason when I use 65, -65 from my characters location as the Raycast direction its casting southish. Even when I use Debug.DrawLine it’s showing a line going to the proper location, but Raycast still doesn’t seem to get the hint.

Raycast takes a STARPOS and a DIRECTION
You are giving it start and DESTINATION
which is wrong

You could try linecast for that

1 Like

That might work. The documentation states there is no way to limit how far the line will cast. I suppose the best way to compare a hit would be if (linecastHit < objectDIstance) then hit = true?

That did the trick! Wasn’t aware that existed. Thank you!