Make an object move from Point A to Point B then back to Point A repeating

Hi

I was wondering how I can move an object from Point A to Point B and then back to Point A. I want to make a prefab out of this so maybe make this relative to its original point.

Thanks, Chris

Similar suggestion to Eric5h5's here, but simplified somewhat, making use of Mathf.PingPong, speed is adjustable in the inspector, and you can use an empty gameobject reference to define the location of PointB.

var pointB : Transform;
private var pointA : Vector3;
var speed = 1.0;
function Start () {
    pointA = transform.position;
    while (true) {
     var i = Mathf.PingPong(Time.time * speed, 1);
     transform.position=Vector3.Lerp(pointA,pointB.position,i);
     yield;
    }
}

(untested, don't have unity in front of me right now)


To save anyone typing, here’s (fully tested) c#

I guess these days you’d almost always use SmoothStep

using UnityEngine;
using System.Collections;
public class SlideBackFore : MonoBehaviour
{
public Transform farEnd;
private Vector3 frometh;
private Vector3 untoeth;
private float secondsForOneLength = 20f;

void Start()
{
frometh = transform.position;
untoeth = farEnd.position;
}

void Update()
{
transform.position = Vector3.Lerp(frometh, untoeth,
 Mathf.SmoothStep(0f,1f,
  Mathf.PingPong(Time.time/secondsForOneLength, 1f)
) );
}
}

Hope it helps

Use a coroutine that moves from one point to another, inside an infinite loop:

var pointB : Vector3;

function Start () {
    var pointA = transform.position;
    while (true) {
        yield MoveObject(transform, pointA, pointB, 3.0);
        yield MoveObject(transform, pointB, pointA, 3.0);
    }
}

function MoveObject (thisTransform : Transform, startPos : Vector3, endPos : Vector3, time : float) {
    var i = 0.0;
    var rate = 1.0/time;
    while (i < 1.0) {
        i += Time.deltaTime * rate;
        thisTransform.position = Vector3.Lerp(startPos, endPos, i);
        yield; 
    }
}

After much Googling this is a working C# version of Eric5h5’s answer:

public Vector3 pointB;
	
	IEnumerator Start()
	{
		var pointA = transform.position;
		while (true) {
	        yield return StartCoroutine(MoveObject(transform, pointA, pointB, 3.0f));
	        yield return StartCoroutine(MoveObject(transform, pointB, pointA, 3.0f));
	    }
	}
 
	IEnumerator MoveObject(Transform thisTransform, Vector3 startPos, Vector3 endPos, float time)
	{
	    var i= 0.0f;
	    var rate= 1.0f/time;
	    while (i < 1.0f) {
	        i += Time.deltaTime * rate;
	        thisTransform.position = Vector3.Lerp(startPos, endPos, i);
	        yield return null; 
	    }
	}

Unlike Javascript, C# requires you to use the StartCoroutine method and any methods to be used as coroutines must return IEnumerator. This page explains how coroutines work.

For Those who need it written in c# i believe this should work.

public Vector3 pointB;

IEnumerator Start () {
    Vector3 pointA = transform.position;
    while (true) {
        yield MoveObject(transform, pointA, pointB, 3.0);
        yield MoveObject(transform, pointB, pointA, 3.0);
    }
}

IEnumerator MoveObject (Transform thisTransform, Vector3 startPos, Vector3 endPos, float time) {
    float i = 0.0f;
    float rate = 1.0f / time;
    while (i < 1.0f) {
        i += Time.deltaTime * rate;
        thisTransform.position = Vector3.Lerp(startPos, endPos, i);
        yield; 
    }
}

Hey, when i need box moving up and down repeatedly, i was achived this by following code in C#:

private Vector3 MovingDirection = Vector3.up;

void Update () {

	gameObject.transform.Translate(MovingDirection * Time.smoothDeltaTime);
		
	if(gameObject.transform.position.y > 3){
		MovingDirection = Vector3.down;
	}else if (gameObject.transform.position.y < -3) {
		MovingDirection = Vector3.up;
	}
}

Show example of How can I call this fucntion?
function MoveObject (thisTransform : Transform, startPos : Vector3, endPos : Vector3, time : float)
(Eric5h5’s func.)

Can someone please convert this to C#? I’m having trouble trying to do it myself.

Though its work great but its not smooth enough,sometimes it feels a bit vibrate.

What to do to avoid it?

you can use animation.
like on 0:00 wil be A position,
on 2:00 wil be B position,
and again on 4:00 wil be again position A.

Thanks duck - I know this is an old post. But if anyone needs a script to move an object up and down smoothly and control the speed this is it: Remember to place an empty gameobject below the object you are moving up and down so it has a something to detect where to stop and go back up. Add the empty object to the inspector and off you go. Nice one!!

using UnityEngine;

using System.Collections;

public class SlideBackFore : MonoBehaviour
{

public Transform farEnd;

private Vector3 frometh;

private Vector3 untoeth;

private float secondsForOneLength = 20f; (Lower this figure for a quicker transition say 5f…)

void Start()
{
frometh = transform.position;
untoeth = farEnd.position;
}

void Update()
{

transform.position = Vector3.Lerp(frometh, untoeth,
Mathf.SmoothStep(0f,1f,
Mathf.PingPong(Time.time/secondsForOneLength, 1f) ) );

}

}