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

 0 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 more ▼ asked Apr 02 '10 at 08:22 AM cmos 467 ● 29 ● 40 ● 54 Ricardo 5.2k ● 20 ● 32 ● 96 Thanks for this, however can someone elaborate on how to set position of PointB? Nov 05 '10 at 11:07 AM keefus Ah, Sorry, just worked it out! Nov 05 '10 at 11:52 AM keefus add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

sort voted first
 0 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; } } `````` more ▼ answered Apr 02 '10 at 08:59 AM Eric5h5 80.3k ● 42 ● 132 ● 521 Thanks!! Works perfectly. Apr 04 '10 at 04:58 AM cmos How is this done in C#? Apr 23 '12 at 12:43 PM reilg no it done in java script Mar 18 at 08:06 PM MRProproduction1 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
 0 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) more ▼ answered Apr 02 '10 at 10:21 AM duck ♦♦ 41k ● 92 ● 148 ● 415 For this one, I get the error: pointB has not been assigned on this line => transform.position = Vector3.Lerp(pointA, pointB.position, i); Apr 02 '10 at 06:00 PM cmos You need to drag & drop a reference to the object which represents PointB into that variable slot in the inspector. Apr 02 '10 at 06:29 PM duck ♦♦ very nice script! Jun 08 '10 at 02:30 AM vdizzle hi... i tried the above script that you posted. However, I noticed that when it reaches point B, it doesnt turn the axis of the object (example, boat). Do you have any idea how should i modify the script? Mar 15 '11 at 07:47 AM coco88 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
 0 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; } } `````` more ▼ answered Oct 21 '12 at 12:15 AM Sooper1337 24 ● 5 ● 7 ● 9 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
 0 Can someone please convert this to C#? I'm having trouble trying to do it myself. more ▼ answered Jul 16 '12 at 01:12 PM Alter 33 ● 4 ● 9 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
 0 Show example of How can I call this fucntion? function MoveObject (thisTransform : Transform, startPos : Vector3, endPos : Vector3, time : float) (Eric5h5's func.) more ▼ answered Jun 15 '11 at 02:44 PM lnker -4 ● 2 ● 2 ● 3 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

### Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

Topics:

x1093
x251
x170

asked: Apr 02 '10 at 08:22 AM

Seen: 18636 times

Last Updated: Mar 18 at 08:06 PM