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

Can't get Time.deltaTime to add-up.

Discussion in 'Scripting' started by Username0101, Sep 20, 2015.

  1. Username0101

    Username0101

    Joined:
    Sep 3, 2015
    Posts:
    18
    Code (CSharp):
    1.  
    2.     void executeMovement() {
    3.         if (done) return;
    4.         prev = new Vector3(transform.position.x, transform.position.y, transform.position.z);
    5.         for (float i = 0.0000f; i < 1.0f; i += Time.deltaTime / 10) {
    6.             transform.position = Vector3.Lerp(prev, MovingTo, i);
    7.             Debug.Log("I: " + i + "| Time.deltaTime: " + Time.deltaTime);
    8.             Debug.Break();
    9.         }
    10.         return;
    11.     }
    Is the code. On Debug.Break...

    I: 0| Time.deltaTime: 0.02
    to:
    I: 0.6339989| Time.deltaTime: 0.02
    up to:
    I: 0.9999942| Time.deltaTime: 0.02

    BEFORE, the Debug.Break(), that means that if user gets to see his/her first frame, object will be already at it's location. Is there a way to let it execute that, but per each frame, instead of before first frame?
     
  2. Firebrand

    Firebrand

    Joined:
    Oct 21, 2013
    Posts:
    9
    From what you've written, and looking at the code, I would imagine you're trying to move an object to a target. However, the way you're going about this issue is completely wrong.

    Code (CSharp):
    1. IEnumerator Start()
    2. {
    3.    while(Vector3.Distance(transform.position, target) > margin)
    4.    {
    5.       transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
    6.       yield return null;
    7.    }
    8. }
    There are a bunch of ways to make something move to a target location. The above is just one example.

    Basically, the script will run until the transform reaches the target. The margin value is at least how the close the transform has to be to the target before it stops moving. Speed is how quickly the transform will move toward it's target. You will need to create variables for both. You could set margin to something like 0.1f and speed to 3f and then tweak the values to get your desired results.

    I hope that helps and I would highly recommend that you look through the Unity API because there is a method/function for basically any problem that you may have.
     
    Last edited: Sep 20, 2015
    Ryiah likes this.
  3. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    1,184
  4. Username0101

    Username0101

    Joined:
    Sep 3, 2015
    Posts:
    18
    I know how Lerp works, 3 variables, 1# from, #2 to, #3 current progress.

    "Coding for the absolute beginner". Nice... I know how movement works, just this script doesn't work out.
     
  5. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    That's not how Lerp is supposed to be used. Start position has to be the same, otherwise you'd need some more math to have a linear interpolation. The only parameter that has to be changed over time is the third one, but not the way you've done it :p
     
    Username0101 likes this.
  6. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    1,184
    Sorry if you took offense. Just trying to be helpful. The problem is, that looking at your sample code, there are so many questions to answer, that it's a better idea to watch one or two of the Unity tutorials which explain movement and all the steps leading to final code than to try and spell it out in text form here in the forum.
     
    Username0101 likes this.
  7. Firebrand

    Firebrand

    Joined:
    Oct 21, 2013
    Posts:
    9
    You're right, I forgot to change Vector3.Lerp to Vector3.MoveTowards. Anyways, code snippet has been edited.
     
    Suddoha likes this.