I have number of objects I want to move them up and forward then when the last one each time in the array get to the end position move this object to the start of the array and to the start position the original start position of the first object.
The idea is to make the objects cyclic on place. Not to move all the objects but to cyclic them up and forward.
I tried this but it's totally wrong. It's moving all the objects as a group to another place and the last one return to the first position big mess.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class MoveObjects : MonoBehaviour
{
public float speed = 1f;
private Vector3[] startPos;
private Vector3[] endPos;
private Vector3[] currentPos;
private GameObject[] objectstoMove;
// Use this for initialization
public void Init()
{
objectstoMove = GameObject.FindGameObjectsWithTag("Stair");
startPos = new Vector3[objectstoMove.Length];
endPos = new Vector3[objectstoMove.Length];
currentPos = new Vector3[objectstoMove.Length];
for(int i = 0; i < objectstoMove.Length; i++)
{
startPos[i] = objectstoMove[i].transform.position;
endPos[i] = new Vector3(objectstoMove[i].transform.position.x + 50, objectstoMove[i].transform.position.y + 50, 0);
}
}
// Update is called once per frame
void Update()
{
if (objectstoMove != null)
{
for(int i = 0; i < objectstoMove.Length; i++)
{
currentPos[i] = objectstoMove[i].transform.position;
objectstoMove[i].transform.Translate(1, 1, speed * Time.deltaTime);
if(currentPos[i].x >= endPos[i].x && currentPos[i].y >= endPos[i].y)
{
SetTransform(objectstoMove[objectstoMove.Length-1]);
}
}
}
}
void SetTransform(GameObject n)
{
n.transform.position = startPos[0];
}
}
Example screenshot:
Each cube should move up and forward following the one above it. The top one when it's reaching to the end position it should move back to the first cube start position. Then the next top cube when reaching the end position it should move to the original first cube start position !
The idea is to simulate some kind of stairs escalator. So the cubes should move in cyclic. The whole cubes not should change position but they should cyclic.
I tried also this for testing: But not working at all.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovingTest : MonoBehaviour
{
public List<Transform> objectstomove = new List<Transform>();
public float speed = 1;
public Vector3 position1;
public Vector3 position2 = new Vector3(2, 2, 2);
private Transform lastobj;
// Use this for initialization
void Start ()
{
position1 = objectstomove[0].position;
lastobj = objectstomove[objectstomove.Count - 1];
}
// Update is called once per frame
void Update ()
{
for (int i = 0; i < objectstomove.Count; i ++)
{
objectstomove[i].position = Vector3.MoveTowards(objectstomove[i].position,
position2, speed * Time.deltaTime);
}
if (lastobj.position.x == 2)
{
lastobj.position = position1;
objectstomove.Remove(lastobj);
objectstomove.Insert(0, lastobj);
lastobj = objectstomove[objectstomove.Count - 1];
for (int i = 0; i < objectstomove.Count; i++)
{
objectstomove[i].position = Vector3.MoveTowards(objectstomove[i].position,
position2, speed * Time.deltaTime);
}
}
}
}