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

Make Lerp smoother

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

  1. Username0101

    Username0101

    Joined:
    Sep 3, 2015
    Posts:
    18
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class asteroidMove : MonoBehaviour {
    5.     Vector3 MoveTo;
    6.     GameObject Asteroid;
    7.     bool final = false;
    8.     int sideStarter;
    9.     Vector3 initPosition;
    10.     float i = 0.0f;
    11.  
    12.     void Start() {
    13.         initPosition = transform.position;
    14.         sideStarter = (transform.position.x == -200) ? 1000 : -200;
    15.         MoveTo = new Vector3 (sideStarter, 0, Random.Range (-100, -700));
    16.         final = true;
    17.     }
    18.  
    19.     void FixedUpdate() {     // Changing to Update() doesn't change anything.
    20.         if (final) {
    21.             transform.position = Vector3.Lerp(initPosition, MoveTo, i);
    22.             i += 0.001f;
    23.             if (transform.position == initPosition) { Debug.Log ("Finished!"); Debug.Break(); }
    24.         }
    25.     }
    26. }
    Script "works". But too fast, as soon as I press "Play" button. The game is paused and Debug log says "Finished". As it should, but no visible operation has been performed. Could someone lean me hand on this one?

    It might be that i, adds up to 1 in 10000 steps within 1 frame. But do you have any idea how to make it slower?
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Not really sure why you're testing against the initial position of the object, instead of the target (MoveTo) position. That's most likely the cause for why your code is ending early.

    Try something like this, use Update instead of FixedUpdate:

    Code (csharp):
    1.  
    2. void Update() {
    3.     if(final) {
    4.         i += Time.deltaTime;
    5.  
    6.         transform.position = Vector3.Lerp(initPosition, MoveTo, i / 5); // change 5 to how many seconds you want it to take
    7.  
    8.         if(transform.position == MoveTo)
    9.         {
    10.             Debug.Log("Finished");
    11.         }
    12.     }
    13. }
    14.  
     
    Username0101 likes this.
  3. Username0101

    Username0101

    Joined:
    Sep 3, 2015
    Posts:
    18
    Yep, your thing did the trick.

    Not really sure why you're testing against the initial position of the object, instead of the target (MoveTo) position.

    What?
    Your code:
    transform.position = Vector3.Lerp(initPosition, MoveTo, i / 5);
    My code:
    transform.position = Vector3.Lerp(initPosition, MoveTo, i);

     
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Not that part, this part:
    Yours
    Code (csharp):
    1.  
    2. if (transform.position == initPosition)
    3.  
    Mine
    Code (csharp):
    1.  
    2. if(transform.position == MoveTo)
    3.  
     
    Username0101 likes this.
  5. Username0101

    Username0101

    Joined:
    Sep 3, 2015
    Posts:
    18
    OH!! Now I know why it's triggered! OH! Welp, that was a stupid human mistake. But you still solved the movement, so kudo's to you.