- Home /
What is wrong with this script ? if duration is 10 why the transform is moving in 11 seconds and not 10 ?
The main goal is to move the transform to a target then from the target back. Using lerp and make ease in out for both sides when moving to the target and when moving back.
I also want to display the seconds the transform passed in the ui text variable durationText but when it's moving to the target it's counting more then 5 and then back more then 10 more 11.
If i display in the durationText the Time.time then in the end i see 21 but it should be 20. If the duration is 10 i want the transform to move to the target in 10 seconds and back in 10 seconds overall 20 seconds.
I'm also not sure this two lines are correct and if i should change them or to make one line of movement instead of two ? The transform.position lines :
 if (t < 0.5)
             {
                 transform.position = Vector3.Lerp(originPosition, target, curve.Evaluate(t * 2));
             }
             else
             {
                 transform.position = Vector3.Lerp(parent.transform.position, target, curve.Evaluate((1 - t) * 2));
             }
The full script :
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
  
 public class MovingTest : MonoBehaviour
 {
     public AnimationCurve curve = AnimationCurve.EaseInOut(0.0f, 0.0f, 1.0f, 1.0f);
     public Transform pos1;
     public Transform pos2;
     public float duration;
     public Text durationText;
  
     private void Start()
     {
         durationText.text = "0";
     }
  
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.G))
         {
             StartCoroutine(GoAndReturn(pos1.position, duration * 2));
         }
     }
  
     IEnumerator GoAndReturn(Vector3 target, float duration)
     {
         var parent = this.transform.parent;
         this.transform.parent = null;
  
         var originPosition = this.transform.position;
  
         var startTime = Time.time;
  
         var t = 0f;
         while (t <= 1)
         {
             t = (float)((Time.time - startTime) / duration);
             durationText.text = t.ToString();
             if (t < 0.5)
             {
                 transform.position = Vector3.Lerp(originPosition, target, curve.Evaluate(t * 2));
             }
             else
             {
                 transform.position = Vector3.Lerp(parent.transform.position, target, curve.Evaluate((1 - t) * 2));
             }
             yield return null;
         }
         this.transform.parent = parent;
         this.transform.localPosition = Vector3.zero;
     }
 }
Answer by Shrimpey · Feb 19 at 11:33 AM
It seems fine though. It's just that you're printing text after adding to t variable, so it is possible to slightly exceed 20f (something like 20.05f is possible, depends on your framerate tbh). If you restructure your while loop, you'll be able to be more accurate:
 while (t <= 1)
          {
              durationText.text = t.ToString();
              if (t < 0.5)
              {
                  transform.position = Vector3.Lerp(originPosition, target, curve.Evaluate(t * 2));
              }
              else
              {
                  transform.position = Vector3.Lerp(parent.transform.position, target, curve.Evaluate((1 - t) * 2));
              }
              t = (float)((Time.time - startTime) / duration);
              yield return null;
          }
so addition goes after the operations are performed. This way operations will only be performed if t
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                