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. Dismiss Notice

convert multiplication from FixedUpdated to Update

Discussion in 'Scripting' started by baptiste, Jul 12, 2014.

  1. baptiste

    baptiste

    Joined:
    Sep 17, 2012
    Posts:
    11
    Hi guys
    Sorry if thie question is silly.

    I would like to convert this old script using Update() instead of FixedUpdate(), but i can't figure how to do.

    This is my old script:

    FixedUpdate(){
    x = x * y;
    }

    So during 1 second, with a framerate of 10 frame per seconds, that do something like that, isn't it?:
    x = x * y * y * y * y * y * y * y * y * y * y;
    or
    x = x * Mathf.Pow(y, 10);


    So i tried something like that but it's look not good.

    Update(){
    frameRate = 10; // this the old frame Rate for the FixedUpdate

    x = x * Mathf.Pow(y, frameRate * Time.deltaTime);
    }

    someone can help me?

    Thanks
     
  2. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    I don't understand, you want to change update so that it runs on a fixed frameRate, is that right?
    you just need Time.deltaTime*frameRate for that.

    lets say Time.deltaTime returns 0.25. so you would have 10*0.25, or 10/4 so you do x *= Mathf.Pow(y, 2.5);

    what i think you want is:
    x *= y*frameRate*Time.deltaTime;
     
    Last edited: Jul 13, 2014
  3. baptiste

    baptiste

    Joined:
    Sep 17, 2012
    Posts:
    11
    Hi Jister

    I want to use this code:

    FixedUpdated(){
    x = x * y;
    }

    In a "Updated" function, instead of a "FixedUpdated". My goal is to get my script time independent.

    I tried your solution:
    I set the fixed time step to 0.16666666 to have a frame rate of 60fps inside an FixedUpdated function.
    And i compared your solution (inside a "Updated" function):

    Updated(){
    frameRate = 60;
    x *= y*frameRate*Time.deltaTime;
    }

    and my old script (inside an "FixedUpdated" function):

    FixedUpdated(){
    x = x * y;
    }

    That's work fine, but when i change the fixed time step to 0.0333333 to have a framerate of 30 fps, and i change to the variable "framerate" to 30 in the script. That doesn't works anymore.
    I can't figure out what i do wrong.
     
  4. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    how are your setting the fixed rate for FixedUpdate()?
     
  5. baptiste

    baptiste

    Joined:
    Sep 17, 2012
    Posts:
    11
    in edit/project Settings/Time, i change the value "fixed timestep" with 1/framerate.
    I want a framerate of 30fps so i put 0.03333333.
     
  6. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    what are the x and y values and what result do you get from x*=y in fixedupdate?
    i try to mimic what you do but don't know what you're looking for.
     
  7. baptiste

    baptiste

    Joined:
    Sep 17, 2012
    Posts:
    11
    it was to move a character.
    x was the character's speed at the last frame, and so at each frame in fixedUpdated i multiply the speed by 0.9 to keep inertia in the character. So it was something like that:
    newSpeed = oldSpeed * inertiaFactor;
    it was simple to do it with a fixed framerate, but now i need it in a update() function
     
  8. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    sorry you should use fixedDeltaTime instead of deltaTime, my mistake ;-)
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Example : MonoBehaviour
    5. {
    6.     public float fixedFrameRate;
    7.     public float x;
    8.     public float y;
    9.     public float xfix;
    10.     public float yfix;
    11.  
    12.     void Update()
    13.     {
    14.         x *=y*Time.fixedDeltaTime*fixedFrameRate;
    15.     }
    16.     void FixedUpdate()
    17.     {
    18.         xfix *=yfix;
    19.     }
    20.  
    21.  
    22. }
    23.  
     
  9. baptiste

    baptiste

    Joined:
    Sep 17, 2012
    Posts:
    11
    For now that's doesn't work.

    I don't understand this last script:

    x *=y*Time.fixedDeltaTime*fixedFrameRate;

    it's look like this code isn't time independent, what happen if the update function is call two time by frame, or one time every 2 frames?
     
  10. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    delta time is the time since the last frame, so if you have 1 frame a second deltatime is going to be 1, if you have 2 frames a second deltatime is 0.5, 100 frames a second 0.01

    If you want to move 10 units/s you just need 10*Time.deltaTime
     
  11. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    if you put this script on an object in the scene, set your fixed timeStep to lets say 0.02 and framerate to 50, 1 for x and 0.9 for y. You'll see both x end up the same value...
    isn't that what you wanted? update and fixedupdate giving the same outcome?