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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question After if statement, else statement refers to all in group and not just the remainder.

Discussion in 'Scripting' started by hstellingwerff, Jul 30, 2021.

  1. hstellingwerff

    hstellingwerff

    Joined:
    Jul 30, 2021
    Posts:
    1
    Hi. I am relatively new to programming and I am programming along with a Youtuber.
    But for my own practice and insight, I tried projecting arrows on a map using a PerlinNoise input (thus between 0 and 1).
    Code (CSharp):
    1.                 if (h.ContinentalDirection >= 5 / 6f)
    2.                 {
    3.                     Vector3 p = hexGO.transform.position;
    4.                     p.y += 10f;
    5.                     GameObject.Instantiate(ArrowPrefab, p, Quaternion.Euler(0, 330, 0), hexGO.transform);
    6.                 }
    7.                 else if (h.ContinentalDirection >= 4 / 6f)
    8.                 {
    9.                     Vector3 p = hexGO.transform.position;
    10.                     p.y += 10f;
    11.                     GameObject.Instantiate(ArrowPrefab, p, Quaternion.Euler(0, 270, 0), hexGO.transform);
    12.                 }
    13.                 else if (h.ContinentalDirection >= 3 / 6f)
    14.                 {
    15.                     Vector3 p = hexGO.transform.position;
    16.                     p.y += 10f;
    17.                     GameObject.Instantiate(ArrowPrefab, p, Quaternion.Euler(0, 210, 0), hexGO.transform);
    18.                 }
    19.                 else if (h.ContinentalDirection >= 2 / 6f)
    20.                 {
    21.                     Vector3 p = hexGO.transform.position;
    22.                     p.y += 10f;
    23.                     GameObject.Instantiate(ArrowPrefab, p, Quaternion.Euler(0, 150, 0), hexGO.transform);
    24.                 }
    25.                 else if (h.ContinentalDirection >= 1 / 6f)
    26.                 {
    27.                     Vector3 p = hexGO.transform.position;
    28.                     p.y += 10f;
    29.                     GameObject.Instantiate(ArrowPrefab, p, Quaternion.Euler(0, 90, 0), hexGO.transform);
    30.                 }
    31.                 else
    32.                 {
    33.                     Vector3 p = hexGO.transform.position;
    34.                     p.y += 10f;
    35.                     GameObject.Instantiate(ArrowPrefab, p, Quaternion.Euler(0, 30, 0), hexGO.transform);
    36.                 }
    But for some reason, it projects the last arrow over all map hexes. Can somebody explain what went wrong?
     
  2. wileyjerkins

    wileyjerkins

    Joined:
    Oct 13, 2017
    Posts:
    77
    not sure how you are calling that code, maybe you are doing it in an update and things are moving so fast all those values are true at some point and it LOOKS like there are arrows everywhere?

    I suggest not write such "verbose code" ... consider this ...

    Code (CSharp):
    1. Vector3 p = hexGO.transform.position;
    2. p.y += 10f;
    3. var v = 0;
    4.  
    5. if (h.ContinentalDirection >= 5 / 6f)
    6.     {
    7.         v = 330;
    8.     }
    9.     else if (h.ContinentalDirection >= 4 / 6f)
    10.     {
    11.         v= 270;
    12.     }
    13.     //etc
    14.              
    15. GameObject.Instantiate(ArrowPrefab, p, Quaternion.Euler(0, v, 0), hexGO.transform);
     
    Bunny83 and Owen-Reynolds like this.
  3. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,303
    Do you know how to debug your code with the debugger?
    It will make solving problems a lot easier than staring at it and trying to spot the logical error

    In your code editor (visual studio for example), you would attach to the unity editor, and set a break point on the first if statement. You would then press play in the unity editor and make it run that piece of code.
    The execution will pause and jump to your code editor when unity hits your breakpoint, and in the code editor you can mouse over variables to see their values.
    You can also execute one line at a time, and see the path that the code execution is taking.
    It's very useful for determining why conditional logic isn't taking the path you are expecting, and for tracking variable values.

     
    Bunny83 likes this.
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,573
    I have to agree with wileyjerkins and Hikiko66. if statements are one of the most fundamental building blocks in most programming languages and I can asure you that if-else-chains work as they should. So it's most likely your input that may not be what you're expecting. Since all your cases only cover positive values, maybe your input value gets negative because all negative values would be covered by the final else.

    Since you seem to have a 60° increase in each case, this could even be solved with an equation. Specifically

    Code (CSharp):
    1. float angle = 30f + 60f * Mathf.Floor(h.ContinentalDirection * 6f);
    This of course assumes "h.ContinentalDirection" is in the range of 0 to 1. Though since we talk about angles and the intervals represent a uniform distribution it should actually extend in both ways and just wrap around even for values smaller than 0 or greater than 1. Though if that solves your issue of course depends on your exact needs which we don't know.

    Note that if your input value comes from Unity's PerlinNoise function, this function does not return a value between 0 and 1, just roughly ^^. It's now even stated in the documentation that it could return values slightly smaller than 0 and slightly larger than 1.

    Also keep in mind that perlinNoise does not give you a uniform distribution since it depends on the position. Also keep in mind that the noise function only works with inputs between 0 and 1. Integer values as input will produce the same value every time.

    So as the others have said, you should do some debugging and check your actual values.
     
    wileyjerkins likes this.