Probelm with moving objects

Hello, I am making a 3d game that has some objects and I want to make these object move in the x axis and have the same path always like going from x=1 to x=10 and then from x=10 to x=1 for example. Can you help me? I am new to unity so please explain it as easy as possible.

You didn’t mention what kind of movement you planned on using, but the simplest form of movement is using transform.Translate.

Any movement but what’s the code to do what I said before?

Unity actually has a couple moving options, like Translate, MoveTowards, SmoothDamp, and Lerp. From what you’ve described, I’d highly recommend using the Lerping Method. I threw together an example Lerp script that explains how it works.

using System.Collections;
using UnityEngine;

public class SimpleLerp : MonoBehaviour
{
    public Transform target; // --- The Thing you want to Move

    //-----------------------------------------------------------------------------------------------------------------------//
    //The Important Stuff --- Vector3 Lerp(Vector3 startPos, Vector3 endPos, float t);
    //-----------------------------------------------------------------------------------------------------------------------//
    public Vector3 startPos; //Start Position when t = 0.
    public Vector3 endPos; //End Position when t = 1.
    [Range(0f, 1f)] public float t; //Value used to interpolate between startPos and endPos
    //-----------------------------------------------------------------------------------------------------------------------//

    public float duration; ///the bigger the value, the slower the object.

    private void Start() ///Start is called once on startup. Used for initializing.
    {
        StartCoroutine(LerpCoroutine());
    }

    IEnumerator LerpCoroutine() //Dont let this scare you, coroutines are pretty simple.
    {
        float time = 0;
        startPos = target.position; //this makes the start position (startPos) wherever the object currently is.

        CreateVisualReferences();//ignore this

        while (time < duration)
        {
            t = (time / duration);
            target.position = Vector3.Lerp(startPos, endPos, t);
            time += Time.deltaTime;
            yield return null;
        }
        target.position = endPos; //resolving values and position after completion;
        t = 1;
    }

    void CreateVisualReferences()//this is just for creating visul references to see the start point and the endpoint.
    {
        GameObject startpoint = GameObject.CreatePrimitive(PrimitiveType.Sphere);
        GameObject endpoint = GameObject.CreatePrimitive(PrimitiveType.Sphere);
        startpoint.transform.position = startPos;
        endpoint.transform.position = endPos;
        startpoint.transform.localScale = new Vector3(.1f, .1f, .1f);
        endpoint.transform.localScale = new Vector3(.1f, .1f, .1f);
        startpoint.GetComponent<Renderer>().material.color = new Color(0, 1, 0, 1);
        endpoint.GetComponent<Renderer>().material.color = new Color(1, 0, 0, 1);
    }
}

However, on using Lerp for movement, you will often see a seemingly much easier script to use. It seems to work just fine, but its really bad practice to use most of the time.

using UnityEngine;

public class HorribleBadPracticeDontDoThisLerp : MonoBehaviour
{
    public Transform target;
    public Vector3 endPos;
    [Range(0f, 1f)] public float t;
    void Update()
    {
        target.position = Vector3.Lerp(target.position, endPos, t);
    }
}

(Ignore the lack of startPos. Its funtionally the same code as the other one, I’ve just skipped the “middle man.” The only reason I included startPos in the SimpleLerp script was for readability.)

Don’t be tempted by how simple it seems to use. its a lot harder to control, eats up processing power, never actually reaches the target, and it leads to all sort of weird physics issues.

Obviously there are caveats for use of the “Bad” script and special times you would want to use it, but not often with Physical Object Movement.

If you are still confused, here are a few good resources that explain lerp probably better than me :stuck_out_tongue:

https://gamedevbeginner.com/the-right-way-to-lerp-in-unity-with-examples/#:~:text=The right way to use Lerp,-Lerp can be&text=A common use for Lerp,to 1 over that duration.
https://www.reddit.com/r/Unity3D/comments/30e1ko/the_correct_way_to_lerp_something_i_found/

And Good Luck on becoming a Dev!