Ginger and Lavender
Ginger and Lavender

Reputation: 468

How do I access specific raycast hits in a 'for' loop?

I have a loop that draws 4 raycasts from the bottom of my character, which are used to detect collision with objects. My issue is that if raycast #1 and raycast #4 are colliding with different objects they return the values from both objects.

I'd like to make it so that I look for a hit on raycast #4 only, and if that doesn't return a hit then I check raycast #3, etc. Once I have a hit I will check for the value of the object with which the raycast is colliding. I tried using RaycastHits[], but I believe this is intended to be used when you want to analyze multiple hits within all raycasts, not just a single raycast.

for (int i = 0; i < VerticalRayCount; i++)
{
    Vector2 rayOrigin = (directionY == -1) ? RaycastOrigin.bottomLeft : RaycastOrigin.topLeft;
    rayOrigin += Vector2.right * (VerticalRaySpacing * i + deltaMovement.x);

    RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.up * directionY, rayLength, collisionMask);

    Debug.DrawRay(rayOrigin, Vector2.up * directionY * rayLength, Color.red);

    if (directionY == -1)
    {
        if (directionX == 1)
        {
            if (i == VerticalRayCount -1)
            {
                if (hit)
                {
                    if (currentPlatform != hit.collider.gameObject)
                    {
                        var platform = hit.collider.gameObject.GetComponent<IPlatform>();
                        currentPlatform = hit.collider.gameObject;
                    }
                }
            }
            else if (i == 0)
            {
                if (hit)
                {
                    if (currentPlatform != hit.collider.gameObject)
                    {
                        var platform = hit.collider.gameObject.GetComponent<IPlatform>();
                        currentPlatform = hit.collider.gameObject;
                    }
                }
            }
        }
    }
}

Upvotes: 0

Views: 930

Answers (1)

derHugo
derHugo

Reputation: 90813

I see two problems in your code:

  • In your for-loop currentPlatform gets allways overwritten by the last hit.
  • But since you check if(currentPlatform != hit.collider.gameObject) it will allways switch between the last two hits if there is more than one. This happens because it hits the first one and sets it. Than the second hit is not equal to the first one so it gets overwritten. So the next time the currentPlatform is still the second hit and gets again overwritten with the first hit of the new loop and always goes on like this.

To avoid the second remove those if(currentPlatform != hit.collidergameObject).

And you should add break; after a hit so the for loop doesn't continue with the other raycasts but rather exits the for loop immediately. So you get only one hit and don't overwrite them.

...
    if(hit)
    {
        var platform = hit.collider.gameObject.GetComponent<IPlatform>();
        currentPlatform = hit.collider.gameObject;

        // here add break so the for loop is not continued
        break;
    }
...

On this way currentPlattform allways has the value of the first hit.

Upvotes: 0

Related Questions