Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

universal script for moving from point A to point B and back

Discussion in 'Scripting' started by DrMrkev, Nov 6, 2015.

  1. DrMrkev

    DrMrkev

    Joined:
    Oct 25, 2015
    Posts:
    19
    Hello everyone. I am a beginner in Unity 3D and C #, and I'm a very simple game where you play as the ball and have gathered yellow and red dice. It occurred to me to do something there like a moving platform or a large block will make you a pancake :D. I tried this:

    void FixedUpdate ()
    {
    if (end == false)
    {
    transform.position = Vector3.Lerp(startPos,endPos,1000f);
    }
    if (end == true)
    {
    transform.position = Vector3.Lerp(endPos,startPos,1000f);
    }
    if (transform.position == endPos)
    {
    end = true;
    }
    if (transform.position == startPos)
    {
    end = false;
    }
    }

    but the platform only flickered on points startPos and endPos as if was moving too fast, so fast that it was both points simultaneously.
    does anyone know what is wrong or how to fix it?
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
  3. DrMrkev

    DrMrkev

    Joined:
    Oct 25, 2015
    Posts:
    19
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Lerp from start to end until percentage is 1 and then set percentage back to 0 and Lerp from end to start.
    Or Lerp from start to end until percentage is 1 and then start subtracting from percentage instead of adding to it.
     
  5. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    Fajlworks and KelsoMRK like this.
  6. DrMrkev

    DrMrkev

    Joined:
    Oct 25, 2015
    Posts:
    19
    can you do me a sample, I somehow do not understand my code currently looks like this
    Code (CSharp):
    1. public class MoveA : MonoBehaviour {
    2.     public Vector3 startPos;
    3.     public Vector3 endPos;
    4.     public float speed;
    5.     private float startTime;
    6.     private float WayDistance;
    7.  
    8.     void Start ()
    9.     {
    10.         startTime = Time.time;
    11.         WayDistance = Vector3.Distance (startPos, endPos);
    12.  
    13.     }
    14.  
    15.     void Update () {
    16.         float distCovered = (Time.time - startTime) * speed;
    17.         float fracWay = distCovered / WayDistance;
    18.         transform.position = Vector3.Lerp (startPos, endPos, fracWay);
    19.     }
    20. }
     
  7. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Code (csharp):
    1.  
    2. float p = 0;
    3.  
    4. void Update()
    5. {
    6.     p = Mathf.PingPong(p, 1);
    7.     transform.position = Vector3.Lerp(startPos, endPos, p);
    8. }
    9.  
     
  8. DrMrkev

    DrMrkev

    Joined:
    Oct 25, 2015
    Posts:
    19
    Brilliant, thank you
     
  9. DrMrkev

    DrMrkev

    Joined:
    Oct 25, 2015
    Posts:
    19
    I looked at the syntax and finally made it.
    otherwise the code of this contribution somehow does not work, I do not know why.
    But I finally figured out how to do it, it's simple ... not to use Lerp
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MoveA : MonoBehaviour {
    5.     public Vector3 startPos;
    6.     public Vector3 endPos;
    7.     public float speed;
    8.     private float step;
    9.     private bool end;
    10.  
    11.     void Update (){
    12.         if (transform.position == startPos) {
    13.             end = false;
    14.         }
    15.         if (transform.position == endPos) {
    16.             end = true;
    17.         }
    18.         if (end == false) {
    19.             step = speed * Time.deltaTime;
    20.             transform.position = Vector3.MoveTowards (transform.position, endPos, step);        }
    21.         if (end == true) {
    22.             step = speed * Time.deltaTime;
    23.             transform.position = Vector3.MoveTowards (transform.position, startPos, step);
    24.         }
    25.     }
    26. }
    27.  
     
  10. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    this is the worst way of doing something simple as looping between two vectors! :)
    the Mathf.PingPong is right only used wrong in the example above.
    try this:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class LoopBetweenVectors : MonoBehaviour {
    5.  
    6.     public Transform pointA, pointB;
    7.     public float speed = 2;
    8.  
    9.     void Update ()
    10.     {
    11.         transform.position = Vector3.Lerp(pointA.position, pointB.position, Mathf.PingPong(Time.time*speed, 1f));
    12.     }
    13. }
    14.