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

Help to understand the concept of frames related to code!

Discussion in 'Scripting' started by Like-A-Sir, Dec 4, 2015.

  1. Like-A-Sir

    Like-A-Sir

    Joined:
    Sep 14, 2015
    Posts:
    33
    Good evening!

    I have some problems related to understanding the way that I have to code movement in unity.I used to work in Code Blocks in C++,and there everything was simple.But in gaming development appears the frame concept.I can't figure out how to use it.For exemple,how does a while loop is executed in Update function(every frame the actions happen till the condition si true,and then after a number of frames the cond. becomes false and it stops;or everything happens during 1 frame).I also experienced some troubles with moving and rigidbody velocity or lerp.I would really appreciate if you will explain me the frame concept related to coding!

    Thank you very much!
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    Update is simply a function that is called on every active object, every frame. It's called as-is, and if you have a while loop, that while loop will execute all in one frame.

    You might imagine that somewhere inside Unity, there is a while loop running all the time. Within this while loop, it runs through all enabled scripts on active objects and calls Update, if it exists. Then it loops through enabled cameras and renders the scene. (There's a bunch of other stuff in between of course. You can find a nice flowchart here with everything.)

    What you're describing sounds like a coroutine in Unity - a function that runs some code, then yields to the engine (letting the engine process the rest of the frame) until the engine tells it to pick up again, and then it goes again, until the function is over. Search the docs for coroutines for more information and sample code.
     
    Kiwasi and Like-A-Sir like this.
  3. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    Hey mate, so what is going on is the Update() method of your Scripts gets executed every frame tick of the game. So you put all logic that needs to happen every frame for a object in Update().

    Than for holding your game state, for data that needs to be saved between frames, you just store that Data on the object as a property or field.
     
    Last edited: Dec 5, 2015
    Kiwasi and Like-A-Sir like this.
  4. Like-A-Sir

    Like-A-Sir

    Joined:
    Sep 14, 2015
    Posts:
    33
    Thank you !

    Now I know that I have to use a coroutine.I Really appreciate you help sir!
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Unity runs a game loop. It looks like this in really heavy pseudo code.

    Code (CSharp):
    1. void Main (){
    2.     while (hasNotQuit){
    3.         UpdateTime();
    4.         UpdateInput();
    5.         while (fixedTime < simulatedTime){
    6.             foreach (Component component in allComponents){
    7.                 component.FixedUpdate();
    8.             }
    9.             PhysX.Update();
    10.         }
    11.         foreach (Component component in allComponents){
    12.             component.Update();
    13.         }
    14.         foreach (Component component in allComponents){
    15.             component.LateUpdate();
    16.         }
    17.         DoRendering();
    18.     }
    19. }
    There are plenty of bits I've skipped out. For a fuller picture see here.

    http://docs.unity3d.com/Manual/ExecutionOrder.html
     
  6. Like-A-Sir

    Like-A-Sir

    Joined:
    Sep 14, 2015
    Posts:
    33

    If a attach a script which uses a coroutine and has a yield WaitForSeconds(2) , it will affect only the frames of the object which has the script attached or all the objects??
     
  7. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    I don't think I can make enough sense of that question to give an adequate answer.
     
  8. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,199
    WaitForSeconds makes that coroutine (IEnumerator method) pause for 2 seconds. The rest of the script is not affected.
     
    Like-A-Sir likes this.
  9. Like-A-Sir

    Like-A-Sir

    Joined:
    Sep 14, 2015
    Posts:
    33
    Thank you! That is exactly what I was expecting.Have a nice day!