Moving GameObjects smoothly on x-axis

Hey, i have a problem with the script i am writing for a game. i want GameObjects that i saved in an array to move along the x-axis smoothly to a destination, what is the best way to implement it, every post i saw about this is using update function for the gameobject, this is not working for me tho. Is there a better way?

This is the only script for this game:

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
public class Rigi : MonoBehaviour
{
    public GameObject[] Akkus, FarbigKraftwerk, FailureLight;
    public Button Generator, button1shadow, button2shadow, button1symbol, button2symbol, button1cable, button2cable;
    public GameObject symbol, shadow, cable;
    int count1, countshadow, countsymbol, countcable, countfailure;
    public Sprite[] spriteSymbols, spriteShadows, spriteCables, LightBulbs;
    public Dictionary<GameObject, List<Sprite>> AkkuDic = new Dictionary<GameObject, List<Sprite>>();
   
   
  
    public void On_Click_Button1()
   
    {
      

        List<Sprite> ActualList ;
        List<Sprite> CompareList = new List<Sprite>();
        CompareList.Add(spriteSymbols[countsymbol]);
        CompareList.Add(spriteShadows[countshadow]);
        CompareList.Add(spriteCables[countcable]);
        AkkuDic.TryGetValue(Akkus[count1], out ActualList);

        if (ActualList.SequenceEqual(CompareList))
        {
            if (count1+1 == Akkus.Length)
            {
                FarbigKraftwerk[count1].GetComponent<BoxCollider2D>().enabled = true;
                Rigidbody2D gameObjectsRigidBody = FarbigKraftwerk[count1].AddComponent<Rigidbody2D>();
                gameObjectsRigidBody.mass = 50;
                StartCoroutine(AttachCoroutine());
                StartCoroutine(MoveLastCoroutine());
            }
            if (count1+1 < Akkus.Length)
            {
                FarbigKraftwerk[count1].GetComponent<BoxCollider2D>().enabled = true;
                Rigidbody2D gameObjectsRigidBody = FarbigKraftwerk[count1].AddComponent<Rigidbody2D>();
                gameObjectsRigidBody.mass = 50;
                StartCoroutine(AttachCoroutine());
                StartCoroutine(MoveCoroutine());
            }
            if (count1+1 > Akkus.Length)
            {

            }
           
        }
        else
        {
            if (countfailure == FailureLight.Length)
            {

            }

            else
            {
                FailureLight[countfailure].GetComponent<SpriteRenderer>().sprite = LightBulbs[1];
                countfailure++;
            }
        }
    }

    public void Awake()
    {
        List<Sprite> mySpritesWind = new List<Sprite>();
        mySpritesWind.Add(spriteSymbols[5]);
        mySpritesWind.Add(spriteShadows[0]);
        mySpritesWind.Add(spriteCables[1]);
        List<Sprite> mySpritesOel = new List<Sprite>();
        mySpritesOel.Add(spriteSymbols[4]);
        mySpritesOel.Add(spriteShadows[2]);
        mySpritesOel.Add(spriteCables[0]);
        List<Sprite> mySpritesWasser = new List<Sprite>();
        mySpritesWasser.Add(spriteSymbols[0]);
        mySpritesWasser.Add(spriteShadows[3]);
        mySpritesWasser.Add(spriteCables[1]);
        List<Sprite> mySpritesSolar = new List<Sprite>();
        mySpritesSolar.Add(spriteSymbols[3]);
        mySpritesSolar.Add(spriteShadows[4]);
        mySpritesSolar.Add(spriteCables[1]);
        List<Sprite> mySpritesKern = new List<Sprite>();
        mySpritesKern.Add(spriteSymbols[1]);
        mySpritesKern.Add(spriteShadows[1]);
        mySpritesKern.Add(spriteCables[0]);
        List<Sprite> mySpritesKohle = new List<Sprite>();
        mySpritesKohle.Add(spriteSymbols[2]);
        mySpritesKohle.Add(spriteShadows[5]);
        mySpritesKohle.Add(spriteCables[0]);

        AkkuDic.Add(Akkus[0], mySpritesWind);
        AkkuDic.Add(Akkus[1], mySpritesOel);
        AkkuDic.Add(Akkus[2], mySpritesWasser);
        AkkuDic.Add(Akkus[3], mySpritesSolar);
        AkkuDic.Add(Akkus[4], mySpritesKern);
        AkkuDic.Add(Akkus[5], mySpritesKohle);

        Akkus[count1].transform.position += new Vector3(-22, 0, 0);


    }
    public void On_Click_Button1shadow()
    {
        countshadow++;

        if (countshadow == spriteShadows.Length)
        {
            countshadow = 0;
        }

        shadow.GetComponent<SpriteRenderer>().sprite = spriteShadows[countshadow];
    }

    public void On_Click_Button2shadow()
    {

        if (countshadow == 0)
        {
            countshadow = spriteShadows.Length;
        }
        countshadow--;
        shadow.GetComponent<SpriteRenderer>().sprite = spriteShadows[countshadow];
    }
    public void On_Click_Button1symbol()
    {
        countsymbol++;

        if (countsymbol == spriteSymbols.Length)
        {
            countsymbol = 0;
        }

        symbol.GetComponent<SpriteRenderer>().sprite = spriteSymbols[countsymbol];
    }

    public void On_Click_Button2symbol()
    {

        if (countsymbol == 0)
        {
            countsymbol = spriteSymbols.Length;
        }
        countsymbol--;
        symbol.GetComponent<SpriteRenderer>().sprite = spriteSymbols[countsymbol];
    }
    public void On_Click_Button1cable()
    {
        countcable++;

        if (countcable == spriteCables.Length)
        {
            countcable = 0;
        }

        cable.GetComponent<SpriteRenderer>().sprite = spriteCables[countcable];
    }

    public void On_Click_Button2cable()
    {

        if (countcable == 0)
        {
            countcable = spriteCables.Length;
        }
        countcable--;
        cable.GetComponent<SpriteRenderer>().sprite = spriteCables[countcable];
    }
  
  
   IEnumerator AttachCoroutine()
    {
        yield return new WaitForSeconds(4);
        FarbigKraftwerk[count1].GetComponent<Rigidbody2D>().isKinematic = true;
        FarbigKraftwerk[count1].transform.parent = Akkus[count1].transform;

    }

    IEnumerator MoveCoroutine()
    {
        yield return new WaitForSeconds(8);
        Akkus[count1].transform.position += new Vector3(-22, 0, 0);
        Akkus[count1 + 1].transform.position += new Vector3(-22, 0, 0);
        count1++;

    }
    IEnumerator MoveLastCoroutine()
    {
        yield return new WaitForSeconds(8);
        Akkus[count1].transform.position += new Vector3(-22, 0, 0);
        count1++;

    }


}

Well, I don’t see anything about moving an object in Update().
I see a coroutine for moving…but it sets the position once and not over time.

Yes right now i just use the instant movement, because that a thing that works, but i would like to change that to a movement over time!

IEnumerator Mover(Vector3 startpos, Vector3 endpos, float timeReq) {
   Vector3 start = startpos;
   Vector3 end = endpos;
   float t = timeReq;

   for(float n = 0; n <= t; n += Time.deltaTime) {
     transform.position = Vector3.Lerp(start, end, n / t);
     yield return null;
   }
}

Try that & see how it goes.

Note: This is just one example of how to move. In Update(), you can adjust the position based on speed * Time.deltaTime.
You could use MoveTowards, etc…

It’s good to know a few options, because sometimes you’ll want one or another.

1 Like

would it also work for moving two gameobjects after each other, eg move first gameobject thats in the middle of the screen to left outside the screen and after that move the second gameobject from the right side to the middle of the screen. seems Enumerators wouldnt work for this. But thanks for your help so far!

Well, as I said there are a number of ways to move something. And to organize the logic about which moves where & when. You’d have to modify that Coroutine a bit to make it try to do what you’re now asking, but it could be done.
As I said, this is just one option. It’s more helpful to learn how & why they work than to just get something that works this once for you, but you don’t know why it works (in my opinion) :slight_smile:

true true, i will try it myself then, thanks for your help! :smile:

Yep, yep. Try some things and come back if it’s not working and show what you’ve come up with :slight_smile: Good luck.

ok i got my desired thing now, thanks alot again, your code worked out great and it wasnt very hard to put the second gameObject to that! Here is the code, sure it could be done more elegant , but this seems to work already :slight_smile:

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
public class Rigi : MonoBehaviour
{
    public GameObject[] Akkus, FarbigKraftwerk, FailureLight;
    public Button Generator, button1shadow, button2shadow, button1symbol, button2symbol, button1cable, button2cable;
    public GameObject symbol, shadow, cable;
    int count1, countshadow, countsymbol, countcable, countfailure;
    public Sprite[] spriteSymbols, spriteShadows, spriteCables, LightBulbs;
    public Dictionary<GameObject, List<Sprite>> AkkuDic = new Dictionary<GameObject, List<Sprite>>();
   
   
  
    public void On_Click_Button1()
   
    {
      

        List<Sprite> ActualList ;
        List<Sprite> CompareList = new List<Sprite>();
        CompareList.Add(spriteSymbols[countsymbol]);
        CompareList.Add(spriteShadows[countshadow]);
        CompareList.Add(spriteCables[countcable]);
        AkkuDic.TryGetValue(Akkus[count1], out ActualList);

        if (ActualList.SequenceEqual(CompareList))
        {
            if (count1+1 == Akkus.Length)
            {
                FarbigKraftwerk[count1].GetComponent<BoxCollider2D>().enabled = true;
                Rigidbody2D gameObjectsRigidBody = FarbigKraftwerk[count1].AddComponent<Rigidbody2D>();
                gameObjectsRigidBody.mass = 50;
                StartCoroutine(AttachCoroutine());
                StartCoroutine(MoveLastCoroutine(new Vector3(0, -7.2f, 0), new Vector3(-22, -7.2f, 0), 3));
            }
            if (count1+1 < Akkus.Length)
            {
                FarbigKraftwerk[count1].GetComponent<BoxCollider2D>().enabled = true;
                Rigidbody2D gameObjectsRigidBody = FarbigKraftwerk[count1].AddComponent<Rigidbody2D>();
                gameObjectsRigidBody.mass = 50;
                StartCoroutine(AttachCoroutine());
                StartCoroutine(MoveFirstCoroutine(new Vector3(0, -7.2f, 0), new Vector3(-22, -7.2f, 0), 3));
                StartCoroutine(MoveSecondCoroutine(new Vector3(22, -7.2f, 0), new Vector3(0, -7.2f, 0), 3));
            }
            if (count1+1 > Akkus.Length)
            {

            }
           
        }
        else
        {
            if (countfailure == FailureLight.Length)
            {

            }

            else
            {
                FailureLight[countfailure].GetComponent<SpriteRenderer>().sprite = LightBulbs[1];
                countfailure++;
            }
        }
    }

    public void Awake()
    {
        List<Sprite> mySpritesWind = new List<Sprite>();
        mySpritesWind.Add(spriteSymbols[5]);
        mySpritesWind.Add(spriteShadows[0]);
        mySpritesWind.Add(spriteCables[1]);
        List<Sprite> mySpritesOel = new List<Sprite>();
        mySpritesOel.Add(spriteSymbols[4]);
        mySpritesOel.Add(spriteShadows[2]);
        mySpritesOel.Add(spriteCables[0]);
        List<Sprite> mySpritesWasser = new List<Sprite>();
        mySpritesWasser.Add(spriteSymbols[0]);
        mySpritesWasser.Add(spriteShadows[3]);
        mySpritesWasser.Add(spriteCables[1]);
        List<Sprite> mySpritesSolar = new List<Sprite>();
        mySpritesSolar.Add(spriteSymbols[3]);
        mySpritesSolar.Add(spriteShadows[4]);
        mySpritesSolar.Add(spriteCables[1]);
        List<Sprite> mySpritesKern = new List<Sprite>();
        mySpritesKern.Add(spriteSymbols[1]);
        mySpritesKern.Add(spriteShadows[1]);
        mySpritesKern.Add(spriteCables[0]);
        List<Sprite> mySpritesKohle = new List<Sprite>();
        mySpritesKohle.Add(spriteSymbols[2]);
        mySpritesKohle.Add(spriteShadows[5]);
        mySpritesKohle.Add(spriteCables[0]);

        AkkuDic.Add(Akkus[0], mySpritesWind);
        AkkuDic.Add(Akkus[1], mySpritesOel);
        AkkuDic.Add(Akkus[2], mySpritesWasser);
        AkkuDic.Add(Akkus[3], mySpritesSolar);
        AkkuDic.Add(Akkus[4], mySpritesKern);
        AkkuDic.Add(Akkus[5], mySpritesKohle);

        StartCoroutine(MoveFirstCoroutine(new Vector3(22, -7.2f, 0), new Vector3(0, -7.2f, 0), 3));

    }
    public void On_Click_Button1shadow()
    {
        countshadow++;

        if (countshadow == spriteShadows.Length)
        {
            countshadow = 0;
        }

        shadow.GetComponent<SpriteRenderer>().sprite = spriteShadows[countshadow];
    }

    public void On_Click_Button2shadow()
    {

        if (countshadow == 0)
        {
            countshadow = spriteShadows.Length;
        }
        countshadow--;
        shadow.GetComponent<SpriteRenderer>().sprite = spriteShadows[countshadow];
    }
    public void On_Click_Button1symbol()
    {
        countsymbol++;

        if (countsymbol == spriteSymbols.Length)
        {
            countsymbol = 0;
        }

        symbol.GetComponent<SpriteRenderer>().sprite = spriteSymbols[countsymbol];
    }

    public void On_Click_Button2symbol()
    {

        if (countsymbol == 0)
        {
            countsymbol = spriteSymbols.Length;
        }
        countsymbol--;
        symbol.GetComponent<SpriteRenderer>().sprite = spriteSymbols[countsymbol];
    }
    public void On_Click_Button1cable()
    {
        countcable++;

        if (countcable == spriteCables.Length)
        {
            countcable = 0;
        }

        cable.GetComponent<SpriteRenderer>().sprite = spriteCables[countcable];
    }

    public void On_Click_Button2cable()
    {

        if (countcable == 0)
        {
            countcable = spriteCables.Length;
        }
        countcable--;
        cable.GetComponent<SpriteRenderer>().sprite = spriteCables[countcable];
    }
  
  
   IEnumerator AttachCoroutine()
    {
        yield return new WaitForSeconds(4);
        FarbigKraftwerk[count1].GetComponent<Rigidbody2D>().isKinematic = true;
        FarbigKraftwerk[count1].transform.parent = Akkus[count1].transform;

    }

    IEnumerator MoveFirstCoroutine(Vector3 startpos, Vector3 endpos, float TimeReq)
    {
        yield return new WaitForSeconds(8);
        Vector3 start = startpos;
        Vector3 end = endpos;
        float t = TimeReq;

        for(float n = 0; n <= t; n += Time.deltaTime)
        {
            Akkus[count1].transform.position = Vector3.Lerp(start, end, n / t);
            yield return null;
        }
   
    }
    IEnumerator MoveSecondCoroutine(Vector3 startpos, Vector3 endpos, float TimeReq)
    {
        yield return new WaitForSeconds(12);
        Vector3 start = startpos;
        Vector3 end = endpos;
        float t = TimeReq;

        for (float n = 0; n <= t; n += Time.deltaTime)
        {
            Akkus[count1+1].transform.position = Vector3.Lerp(start, end, n / t);
            yield return null;
        }
        count1++;
    }
    IEnumerator MoveLastCoroutine(Vector3 startpos, Vector3 endpos, float TimeReq)
    {
        yield return new WaitForSeconds(8);
        Vector3 start = startpos;
        Vector3 end = endpos;
        float t = TimeReq;

        for (float n = 0; n <= t; n += Time.deltaTime)
        {
            Akkus[count1].transform.position = Vector3.Lerp(start, end, n / t);
            yield return null;
        }
        count1++;
    }


}

Thanks again and have a nice day!!

Okay, sure, glad I could help and that it’s working for you. Have a nice day, yourself :slight_smile: