Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
12
Question by cmos · Apr 02, 2010 at 08:22 AM · objectpathfindingwaypoint

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

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image keefus · Nov 05, 2010 at 11:07 AM 0
Share

Thanks for this, however can someone elaborate on how to set position of PointB?

avatar image keefus · Nov 05, 2010 at 11:52 AM 0
Share

Ah, Sorry, just worked it out!

avatar image miguelcosta217 · Mar 17, 2015 at 09:19 PM 0
Share

a question, and if I only want to move the object horizontally?

10 Replies

· Add your reply
  • Sort: 
avatar image
22
Best Answer

Answer by Eric5h5 · Apr 02, 2010 at 08:59 AM

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; 
     }
 }            
Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cmos · Apr 04, 2010 at 04:58 AM 1
Share

Thanks!! Works perfectly.

avatar image reilg · Apr 23, 2012 at 12:43 PM 1
Share

How is this done in C#?

avatar image MRProproduction1 · Mar 18, 2013 at 08:06 PM 0
Share

no it done in java script

avatar image Zarenityx · Jun 14, 2013 at 07:51 PM 0
Share

He said 'How' not 'Is'. The answer is probably to use StartCoroutine(), athough I am a lot better at JS than I am at C#.

avatar image buttmatrix · Apr 16, 2015 at 05:18 AM 0
Share

{resolved}

Show more comments
avatar image
22

Answer by duck · Apr 02, 2010 at 10:21 AM

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

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cmos · Apr 02, 2010 at 06:00 PM 0
Share

For this one, I get the error: pointB has not been assigned on this line => transform.position = Vector3.Lerp(pointA, pointB.position, i);

avatar image duck ♦♦ · Apr 02, 2010 at 06:29 PM 1
Share

You need to drag & drop a reference to the object which represents PointB into that variable slot in the inspector.

avatar image vdizzle · Jun 08, 2010 at 02:30 AM 0
Share

very nice script!

avatar image malodia · Apr 02, 2016 at 12:24 AM 0
Share

how to increase the speed of transfor$$anonymous$$g postion please help!

avatar image KonnyKarlito malodia · Apr 11, 2016 at 09:38 PM 0
Share

at this line private float secondsForOneLength = 20f; this number is seconds of travel lenght...

avatar image audegames · Jul 04, 2016 at 10:14 PM 0
Share

how to add more than one point? and make it repeat?

Show more comments
avatar image
8

Answer by philjhale · Jul 24, 2013 at 09:34 PM

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Benr223 · Aug 14, 2014 at 01:19 PM 0
Share

Hi, I have been trying to use this to move a player to an enemy on click of a button, with that in $$anonymous$$d i would not be able to use the Start function and would like this to happen on attack. as the OnGUI method cannot return a IEnumerator, I have tried to take all of the code in the example of start, and put it in a separate method. Then i call this method inside of my GUI.Button. It doesnt seem to work? can anyone provide an explanation?

avatar image BManson · Feb 27, 2015 at 12:29 AM 0
Share

This Code really helped me out, Thank You!

avatar image Fcaldas · Aug 08, 2015 at 12:42 PM 0
Share

This is great, thank you

avatar image Jasmin1347 · Feb 15, 2018 at 04:47 AM 0
Share

can anyone explain this code , how it works?

avatar image
4

Answer by Sooper1337 · Oct 21, 2012 at 12:15 AM

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; 
     }
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image woodygoody · Jun 01, 2013 at 09:11 AM 0
Share

i have two question in the next code:

while (true) what is 'true' refer to ?

yield $$anonymous$$oveObject(transform, pointA, pointB, 3.0); why use yield before function moveObject

**var pointB : Vector3;

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

function $$anonymous$$oveObject (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; } }*

avatar image philjhale · Jul 24, 2013 at 08:49 PM 0
Share

Sorry to be so blunt but this code snippet isn't valid C#. It doesn't compile

avatar image dcarrier · Jul 30, 2013 at 04:34 PM 0
Share

This works:

 public Vector3 pointB;
  
 IEnumerator Start () {
     Vector3 pointA = transform.position;
     while (true) {
         yield return StartCoroutine($$anonymous$$oveObject(transform, pointA, pointB, 3.0));
         yield return StartCoroutine($$anonymous$$oveObject(transform, pointB, pointA, 3.0));
     }
 }
  
 IEnumerator $$anonymous$$oveObject (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 return null; 
     }
 }
avatar image
4

Answer by Jviaches · Jan 31, 2013 at 01:44 PM

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;
     }
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image aSaDaliii · Jan 23, 2015 at 02:19 PM 0
Share

thanks its working

avatar image miguelcosta217 · Mar 17, 2015 at 08:51 PM 0
Share
 private Vector3 $$anonymous$$ovingDirection = Vector3.right;
 
     
     void Update () {
         gameObject.transform.Translate($$anonymous$$ovingDirection * Time.smoothDeltaTime);
         
         if(gameObject.transform.position.x > -10){
             $$anonymous$$ovingDirection = Vector3.right;
         }else if (gameObject.transform.position.x < 10) {
             $$anonymous$$ovingDirection = Vector3.left;
         }
     }

i try that but dont work :/

avatar image bx00xd · Jun 06, 2017 at 08:42 PM 0
Share

Expanding on the answer of Jviaches: I did include a public function for the upper and the lower positions and the speed of the object to better control the movement of the object from within the Unity editor: using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class SmartCatController : $$anonymous$$onoBehaviour {
     private Vector3 $$anonymous$$ovingDirection = Vector3.up;
     public float Uplimit = 3.0F;
     public float Downlimit = -3.0F;
     public float $$anonymous$$ovementSpeed = 2.0F;
 
     void Update () {
         gameObject.transform.Translate($$anonymous$$ovingDirection * Time.deltaTime * $$anonymous$$ovementSpeed);
 
         if(gameObject.transform.position.y > Uplimit){
             $$anonymous$$ovingDirection = Vector3.down;
         }else if (gameObject.transform.position.y < Downlimit) {
             $$anonymous$$ovingDirection = Vector3.up;
         }
     }
 }    
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

29 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why is my Queue pf nodes being emptied? 0 Answers

How to move an object with a starting and ending speed and time 0 Answers

Advance Colision Detection 1 Answer

Obstacle Spawner Efficiency 0 Answers

why does this happen when i get to close with my camera 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges