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
Thanks for this, however can someone elaborate on how to set position of PointB?
a question, and if I only want to move the object horizontally?
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;
}
}
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#.
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
For this one, I get the error: pointB has not been assigned on this line => transform.position = Vector3.Lerp(pointA, pointB.position, i);
You need to drag & drop a reference to the object which represents PointB into that variable slot in the inspector.
how to increase the speed of transfor$$anonymous$$g postion please help!
at this line private float secondsForOneLength = 20f; this number is seconds of travel lenght...
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.
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?
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;
}
}
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; } }*
Sorry to be so blunt but this code snippet isn't valid C#. It doesn't compile
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;
}
}
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;
}
}
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 :/
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;
}
}
}
Your answer
Follow this Question
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