Search Unity

AI Turning

Discussion in 'Scripting' started by XZ001, May 21, 2012.

  1. XZ001

    XZ001

    Joined:
    May 21, 2011
    Posts:
    166
    I'm writing a script for an AI character to explore. It causes the AI to turn at random times, but I couldn't get one part to work. I tried using a while loop to make the character rotate over a period of time. Here's the script.

    Code (csharp):
    1. while (turning > 0){
    2.     transform.Rotate(0, direction * Time.deltaTime, 0);
    3.     turning -= 1.0 * Time.deltaTime;
    4.     }
    I did this mostly off an assumption it would work, but I dont use loops very often. When the character starts to turn the turning variable is set to 10.0 and is supposed to count down from there. But instead the variable is immediately set to zero. Any suggestions?
     
  2. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    change while to if

    ie... if(turning > 0) {

    }
     
  3. Zethariel1

    Zethariel1

    Joined:
    Mar 21, 2012
    Posts:
    439
    The reason why you are getting your result is because the while loop executes in one frame. What you want to do is put an if statement in the Update function, which execues this once per frame, which is what you want.

    Loops are viable when you are going through arrays or lists, such as handling the turning of 100 enemies by direction * Time.deltaTime each frame.

    Hope this clears things up a bit!
     
  4. XZ001

    XZ001

    Joined:
    May 21, 2011
    Posts:
    166
    I didnt use an if statement, because that only makes the variable "turning" countdown once and it then returns to the beginning of the Update, so the AI only turns for 0.6 seconds which isn't what I want.
     
  5. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    lolwut.

    You want the char to be turning within the update function. The update function is calling as fast as it can, so you want to be turning by a small amount everyframe, which is what your code does (assuming its in the update function).

    if its not turning long enough, send it more time.. otherwise post more code.
     
  6. XZ001

    XZ001

    Joined:
    May 21, 2011
    Posts:
    166
    The amount of time I give it isnt a factor. Even if I change ten seconds to one thousand it still only turns for half a second.
     
  7. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    post more code.
     
  8. XZ001

    XZ001

    Joined:
    May 21, 2011
    Posts:
    166
    That's the only part of the code that's not working. I can run it by itself by giving values to the variables in it and it still doesnt work. I don't think posting more code will help, just confuse people. Its really not related to anything else.
     
  9. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    if you don't want to post code, you're not going to get the help you need. Its a guessing game without seeing the entire script.

    your choice.
     
  10. Poya

    Poya

    Joined:
    Jul 3, 2011
    Posts:
    51
    XZ001: did you actually try what was suggested and use the if? Pretty sure that would fix the problem, unless your update has other logic in it as jlcnz suggested.

    Also for this type of thing (performing an action over a number of frames) I highly suggest using coroutines. There is a lot of documentation and forum posts about it which you can search for.
     
  11. pus2meong

    pus2meong

    Joined:
    May 3, 2012
    Posts:
    83
    In Unity, "While" command is executed in one frame (different from 3DGS). So in one frame, the program will do the count down of turning variable. This will causing the turning variable got zero instantly in a frame.

    Use, "If" command.
    Or post more detail information regarding your code