Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

if statement bug or something mystic at the core

Discussion in '2D' started by Helladah, Aug 14, 2019.

  1. Helladah

    Helladah

    Joined:
    May 5, 2018
    Posts:
    254
    Hi, im trying to achive a random initial direction for a gameobject but while any of the statements of translate would work on his own, it dosent work with the conditions

    Code (CSharp):
    1. Dado();
    2.  
    3.         if (dado == 0) // stop
    4.         {
    5.             transform.Translate(0, 0, 0);
    6.  
    7.         }else if (dado == 1) // up
    8.         {
    9.             transform.Translate(0, speed * Time.deltaTime, 0);
    10.         }
    11.         else if (dado == 2) // up-right
    12.         {
    13.             transform.Translate(speed * Time.deltaTime, speed * Time.deltaTime, 0);
    14.         }
    15.         else if (dado == 3) // down-right
    16.         {
    17.             transform.Translate(speed * Time.deltaTime , -speed * Time.deltaTime, 0);
    18.         }
    19.         else if (dado == 4) // down
    20.         {
    21.             transform.Translate(0, -speed * Time.deltaTime, 0);
    22.         }
    23.         else if (dado == 5) // right
    24.         {
    25.             transform.Translate(speed * Time.deltaTime, 0, 0);
    26.         }
    27.         else if (dado == 6) // down-left
    28.         {
    29.             transform.Translate(-speed * Time.deltaTime, -speed * Time.deltaTime, 0);
    30.         }
    31.         else if (dado == 7) //left
    32.         {
    33.             transform.Translate(-speed * Time.deltaTime, 0, 0);
    34.         }
    35.         else if (dado == 8) // left-up
    36.         {
    37.             transform.Translate(-speed * Time.deltaTime, speed * Time.deltaTime, 0);
    38.         }
    what am i doing wrong buddies?
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    The way you wrote the if statements is correct, I'd suggest using a switch statement though.

    How does the variable "dado" get set? What does the function "Dado()" do?

    I think a nicer solution would be to randomly generate the direction rather than an index:
    Code (CSharp):
    1. public float speed;
    2. private Vector2 direction;
    3.  
    4. private void Start()
    5. {
    6.     direction = GetRandomCardinalDirection();
    7. }
    8.  
    9. private void Update()
    10. {
    11.     transform.Translate(direction * speed * Time.deltaTime);
    12. }
    13.  
    14. private Vector2 GetRandomCardinalDirection()
    15. {
    16.     // randomly choose -1, 0, or 1 for X and Y
    17.     int directionX = UnityEngine.Random.Range(-1, 2);
    18.     int directionY = UnityEngine.Random.Range(-1, 2);
    19.  
    20.     return new Vector2(directionX, directionY).normalized;
    21. }
     
    Helladah likes this.
  3. Helladah

    Helladah

    Joined:
    May 5, 2018
    Posts:
    254
    dado its a random range, thanks a lot