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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

While Loop Issues

Discussion in 'Scripting' started by Browdaddy96, Jul 22, 2016.

  1. Browdaddy96

    Browdaddy96

    Joined:
    Aug 27, 2015
    Posts:
    82
    So I want my AI to move to a certain point based on the player's position which is called in update so I used a while loop but when the AI is almost there the game freezes.

    Code (CSharp):
    1. public void MoveToLeftNodeOnPlatform()
    2.     {
    3.         Vector3 leftNode = controller.objectStandingOn.GetComponent<Ground>().leftNode;
    4.  
    5.         while(transform.position.x >= controller.objectStandingOn.GetComponent<Ground>().leftNode.x)
    6.         {
    7.             SetDirectionalInput(new Vector2(-1, 0));
    8.         }
    9.  
    10.         SetDirectionalInput(new Vector2(0, 0));
    11.     }
    Thank You :)
     
  2. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    definitely looks like an infinite while loop situation, but there's not enough context really to tell why the condition is always true.

    What does SetDirectionalInput do?
    What is Ground.leftNode? Does it change?
    Does transform.position change?
     
  3. Browdaddy96

    Browdaddy96

    Joined:
    Aug 27, 2015
    Posts:
    82
    SetDirectionalInput is a function that set the input that moves the enemy
    Ground.leftNode is the left most position on the ground, it does not move
    transform.position will change when SetDirectionalInput is not Vector2.zero
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    You may want to post your setDirectionalInput as well as I agree, it sounds like you're hitting an infinite loop somewhere.
     
  5. Browdaddy96

    Browdaddy96

    Joined:
    Aug 27, 2015
    Posts:
    82
    Code (CSharp):
    1. public void SetDirectionalInput(Vector2 input)
    2.     {
    3.         directionalInput = input;
    4.     }
    what it is used for in PLAYER'S function:

    Code (CSharp):
    1. void Update()
    2.     {
    3.         Vector2 directionalInput = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
    4.         player.SetDirectionalInput(directionalInput);
    5.  
    6.         if (Input.GetKey(KeyCode.Space))
    7.         {
    8.             player.OnJumpInputDown();
    9.         }
    10.         if (Input.GetKeyUp(KeyCode.Space))
    11.         {
    12.             player.OnJumpInputUp();
    13.         }
    14.     }
     
  6. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    It looks like your "SetDirectionInput" function sets how far the object should move when it does move, but it does not make the object move right away. If that's the case, you keep setting the direction inside the while loop, but you never give the object a chance to move so the condition is always true and it keeps looping.