Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug Raycast goes horizontal instead of down

Discussion in 'Scripting' started by TheHardliner, May 20, 2023.

  1. TheHardliner

    TheHardliner

    Joined:
    Jun 29, 2017
    Posts:
    20
    Hello,
    I have a strange problem.

    In order to change the terrainheight to the collider of a trench I have made this Code

    Code (CSharp):
    1. for (float TX = minx; TX < maxx; TX = TX + 0.1f)
    2.         {
    3.             for (float TZ = minz; TZ < maxz; TZ = TZ + 0.1f)
    4.             {
    5.                 Vector3 Rayorigin = new Vector3(TX, startPos.y, TZ);
    6.                 Ray ray = new Ray(Rayorigin, Vector3.down);
    7.                 RaycastHit hit;
    8.  
    9.                 if (collider.Raycast(ray, out hit, 10))
    10.                 {
    11.                     //Debug.DrawLine(Rayorigin, hit.point, Color.green, 5);
    12.                     Debug.Log("Good hit: origin: " + Rayorigin + " hit: " + hit.point);
    13.                     float yHit = hit.point.y;
    14.                     var height = yHit / terrainSize.y;
    15.                     //temporary
    16.                     height -= 0.5f;
    17.                     int tery = (int)(TX * umrechnung);
    18.                     int terx = (int)(TZ * umrechnung);
    19.                     heightMapCurrent[terx, tery] = height;
    20.                 }
    21.                 else
    22.                 {
    23.                     Debug.DrawLine(Rayorigin, hit.point, Color.red, 5);
    24.                     Debug.Log("No hit: origin: " + Rayorigin + " hit: " + hit.point);
    25.                 }
    26.             }
    27.         }
    I wondered why it doesn't work sometimes and later I figured out that the ray isn't going down but horizontal. But I have no idea why.

    When it works:

    Same x and z values:

    When it doesn't work:

    or:


    I noticed that a new start of unity will solve this problem partially.

    How on earth can the direction of the ray be changed when there is "Vector3.down"? I have no idea.
    You?:)
     
  2. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,040
    I don't get why you're trying to find meaning behind a point that is a result of no hit.

    There are no guarantees that the hit point will contain anything of value, the reason: there is no hit.
    You detect this and then print out these junk values.

    This has nothing to do with the orientation of the ray. It is still pointing down.
     
    MelvMay likes this.
  3. TheHardliner

    TheHardliner

    Joined:
    Jun 29, 2017
    Posts:
    20
    I want to find out why my code isn't working properly. The elsepath is just for debugging. To me it seems it isn't working because the ray is not going down therefore it can't hit the collider which is underneath the origin.

    Or: I want to see where the ray is going when it should hit the collider but it isn't.
     
  4. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,040
    But it says right there "no hit" and indeed you print this out only when there is no hit.
    And you have observed yourself that when it does hit, there is nothing wrong with the values.
    It is your observation that "raycast goes horizontal instead of down" that is misleading you.
    You simply don't hit anything, and the reasons why this is so are beyond that piece of code.

    In any case you really don't want to draw nonsensical lines that connect invalid junk points.
    If you want to draw a ray I believe there is a DrawRay method in the Gizmos class or you can make your own:
    Code (csharp):
    1. Debug.DrawLine(ray.origin, ray.origin + ray.direction, Color.red, 5);
    You can also make it longer if you want to, just multiply ray.direction with some scalar
    Code (csharp):
    1. Debug.DrawLine(ray.origin, ray.origin + length * ray.direction, Color.red, 5);
     
    MelvMay likes this.
  5. TheHardliner

    TheHardliner

    Joined:
    Jun 29, 2017
    Posts:
    20
    Hello,
    I'm sorry for the late reply. Had a lot of other troubles going on.

    Ah ok now I get it. This kind of debugging was wrong in the first place.

    But I managed to find the error. Well..like most times it was elsewhere.

    Thank you for your help!
     
    orionsyndrome likes this.