Search Unity

Question Probelm with moving objects

Discussion in 'Getting Started' started by JohnXanthos, Apr 27, 2021.

  1. JohnXanthos

    JohnXanthos

    Joined:
    Apr 26, 2021
    Posts:
    2
    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.
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
  3. JohnXanthos

    JohnXanthos

    Joined:
    Apr 26, 2021
    Posts:
    2
  4. FaffyWaffles

    FaffyWaffles

    Joined:
    Apr 10, 2020
    Posts:
    45
    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.
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3.  
    4. public class SimpleLerp : MonoBehaviour
    5. {
    6.     public Transform target; // --- The Thing you want to Move
    7.  
    8.     //-----------------------------------------------------------------------------------------------------------------------//
    9.     //The Important Stuff --- Vector3 Lerp(Vector3 startPos, Vector3 endPos, float t);
    10.     //-----------------------------------------------------------------------------------------------------------------------//
    11.     public Vector3 startPos; //Start Position when t = 0.
    12.     public Vector3 endPos; //End Position when t = 1.
    13.     [Range(0f, 1f)] public float t; //Value used to interpolate between startPos and endPos
    14.     //-----------------------------------------------------------------------------------------------------------------------//
    15.  
    16.     public float duration; ///the bigger the value, the slower the object.
    17.  
    18.     private void Start() ///Start is called once on startup. Used for initializing.
    19.     {
    20.         StartCoroutine(LerpCoroutine());
    21.     }
    22.  
    23.     IEnumerator LerpCoroutine() //Dont let this scare you, coroutines are pretty simple.
    24.     {
    25.         float time = 0;
    26.         startPos = target.position; //this makes the start position (startPos) wherever the object currently is.
    27.  
    28.         CreateVisualReferences();//ignore this
    29.  
    30.         while (time < duration)
    31.         {
    32.             t = (time / duration);
    33.             target.position = Vector3.Lerp(startPos, endPos, t);
    34.             time += Time.deltaTime;
    35.             yield return null;
    36.         }
    37.         target.position = endPos; //resolving values and position after completion;
    38.         t = 1;
    39.     }
    40.  
    41.     void CreateVisualReferences()//this is just for creating visul references to see the start point and the endpoint.
    42.     {
    43.         GameObject startpoint = GameObject.CreatePrimitive(PrimitiveType.Sphere);
    44.         GameObject endpoint = GameObject.CreatePrimitive(PrimitiveType.Sphere);
    45.         startpoint.transform.position = startPos;
    46.         endpoint.transform.position = endPos;
    47.         startpoint.transform.localScale = new Vector3(.1f, .1f, .1f);
    48.         endpoint.transform.localScale = new Vector3(.1f, .1f, .1f);
    49.         startpoint.GetComponent<Renderer>().material.color = new Color(0, 1, 0, 1);
    50.         endpoint.GetComponent<Renderer>().material.color = new Color(1, 0, 0, 1);
    51.     }
    52. }

    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.


    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class HorribleBadPracticeDontDoThisLerp : MonoBehaviour
    5. {
    6.     public Transform target;
    7.     public Vector3 endPos;
    8.     [Range(0f, 1f)] public float t;
    9.     void Update()
    10.     {
    11.         target.position = Vector3.Lerp(target.position, endPos, t);
    12.     }
    13. }
    14.  
    (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 :p

    https://chicounity3d.wordpress.com/2014/05/23/how-to-lerp-like-a-pro/
    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/
    https://docs.unity3d.com/ScriptReference/Vector3.Lerp.html


    And Good Luck on becoming a Dev!
     
    Last edited: May 1, 2021