Question by 
               shapovalovletterbox · Dec 25, 2019 at 03:43 AM · 
                c#unity 5transformtime.deltatimetransform.translate  
              
 
              C# I need to move object in a cycle
Hello! I need to move object first upwards, and after that, downwards, and than repeat. I am experiencing some troubles with making a workable cycle. At this moment it tried using for counter, which was pretty bad idea and there were no progress:
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 
 
 
 public class CubeMoving : MonoBehaviour
 {
     public int i;
     public int a;
     public int x;
     //Ниже объявляются переменные для работы с таймером
 
   
 
     void Start()
     {
 
     }
 
     // Update is called once per frame
 
     void Update()
     {
         for (i = 0; i < 20; i++); 
         {
             transform.Translate(0, 0.01f, 0);
             print("1st is done");
         }
 
         //yield return new WaitForSeconds(5);
 
         for (a = 0; a < 20; a++);
         {
             transform.Translate(0, -0.01f, 0);
             print("2nd is done");
         }
 
     }
 }
Earlier, I tried using Deltatime, but it didn't worked too.
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 
 
 
 public class CubeMoving : MonoBehaviour
 {
     public GameObject CubeObject;
     public Vector3 Position;
 
     //Ниже объявляются переменные для работы с таймером
 
     private float waitTime = 4.0f; //4 секунды по умолчанию
     private float timer = 0.0f;
     private float visualTime = 0.0f; //Что это?
   
 
     void Start()
     {
 
     }
 
     // Update is called once per frame
 
     void Update()
     {
         //Используем таймер, чтобы управлять движением блока
         timer = Time.deltaTime; //Получаем текущее значение времени на основе фреймов
 
         if (timer > 2) //Подъём вверх
         {
             transform.Translate(Vector3.up * 0.001f, Space.World);
         }
 
         if (timer>=2 && timer<=4) //
         {
             transform.Translate(Vector3.down *0.001f, Space.World);
         }
         //Спуск вниз
 
         if (timer > waitTime) //Заканчиваем чх секундный цикл
         {
             timer = 0.0f;
 
             // Remove the recorded 2 seconds.
             timer = timer - waitTime;
             
         } 
 
     }
 }
 
How do I solve my riddle? Thank you!
               Comment
              
 
               
              This is a rough code but give it a try. Let me know if it works the way you wanted.
 public class Test$$anonymous$$oveObjectUpDown : $$anonymous$$onoBehaviour
 {
     [SerializeField]
     private float _distanceFromOrigin = 5;
 
     [SerializeField]
     private float _speed = 2; //Change this to increase the speed
 
     private Vector3 _startPosition;
 
     [SerializeField]
     private bool _is$$anonymous$$ovingUp;
 
     private void Start()
     {
         _startPosition = gameObject.transform.position;
     }
 
     private void Update()
     {
         if (_is$$anonymous$$ovingUp)
         {
             gameObject.transform.position = Vector3.$$anonymous$$oveTowards(gameObject.transform.position, new Vector3(_startPosition.x, _startPosition.y + _distanceFromOrigin, _startPosition.z), Time.deltaTime * _speed);
             if (gameObject.transform.position.y >= _startPosition.y + _distanceFromOrigin)
                 _is$$anonymous$$ovingUp = false;
         }
         else
         {
             gameObject.transform.position = Vector3.$$anonymous$$oveTowards(gameObject.transform.position, new Vector3(_startPosition.x, _startPosition.y - _distanceFromOrigin, _startPosition.z), Time.deltaTime * _speed);
             if (gameObject.transform.position.y <= _startPosition.y - _distanceFromOrigin)
                 _is$$anonymous$$ovingUp = true;
         }
     }
 }
I believe the only fix you need is to replace
 timer = Time.deltaTime;
By
 timer += Time.deltaTime;
Or you can just do
 float ratio = $$anonymous$$athf.PingPong(Time.time / cycleDuration, 1f)
 transform.position = Vector3.Lerp(startPos, endPos, ratio);
Your answer
 
 
             Follow this Question
Related Questions
How to fix snake behavior in Unity3d? 0 Answers
Camera isn't move position? Why my camera isn't change position? 0 Answers
How can I make this character move smoothly? 1 Answer
Problems With .rotate behavior 1 Answer
Time.deltatime not working. 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                