How to animate a list of GameObject from their localposition

Hi everybody !

I got a problem, I’m trying to animate a list of GameObject from their localposition.
The goal is to create a kind of levitation of those objects, like the pod in starwars.

I’m trying to store the initial position to go back to her after a move to never be to far from my original point.

I think I’m in a wrong way and after like aaaaaaaaaall day long I’m still in the same point.

Here is the code I wrote :

using UnityEngine;
using System.Collections;

public class Levitation : MonoBehaviour {
	
	private GameObject[] Planes;
	
	private Vector3[] PosInitiales;
	private Vector3[] PosNew;
	
	private float randamFloatX;
	private float randamFloatY;
	private float randamFloatZ;
	
	private bool NewValue = true;
	
	private float T;
	private float timeToWait = 1f;
	private float startTime;

	void Awake(){
		Planes = GameObject.FindGameObjectsWithTag ("LevitationObject");
		
		PosInitiales = new Vector3[Planes.Length];
		
		for (int i = 0; i < Planes.Length; i++) {
			PosInitiales _= Planes*.transform.localPosition;*_

* }*

* }*

* // Use this for initialization*
* void Start () {*
* randamFloatX = Random.Range(-0.1f, 0.1f);*
* randamFloatY = Random.Range(-0.1f, 0.1f);*
* }*

* // Update is called once per frame*
* void Update () {*
* if(NewValue == true){*
* StartCoroutine ( WaitAndMove(timeToWait));*
* startTime = Time.time;*
* }*

* T = (Time.time - startTime) / 1f;*

* foreach(GameObject PlanesToMove in Planes){*
* PosNew = new Vector3[Planes.Length];*
* for (int ib = 0; ib < Planes.Length; ib++) {*
* PosNew[ib] = Planes[ib].transform.localPosition;*

* for (int i = 0; i < Planes.Length; i++) {*
_ if(PosNew[ib] != PosInitiales*){*_

_ PlanesToMove.transform.localPosition = PosInitiales*;
}
if(PosNew[ib] == PosInitiales){
float lerpX = Mathf.Lerp(PlanesToMove.transform.localPosition.x, (PlanesToMove.transform.localPosition.x+randamFloatX), T);
float lerpY = Mathf.Lerp(PlanesToMove.transform.localPosition.y, (PlanesToMove.transform.localPosition.y+randamFloatY), T);*_

* PlanesToMove.transform.localPosition = new Vector3(lerpX, lerpY, PlanesToMove.transform.localPosition.z);*
* }*
* }*
* }*
* }*
}
* IEnumerator WaitAndMove(float waitTime){*
* NewValue = false;*
* yield return new WaitForSeconds(waitTime);*
* randamFloatX = Random.Range(-0.1f, 0.1f);*
* randamFloatY = Random.Range(-0.1f, 0.1f);*
* NewValue = true;*
* }*

}
Help me before I start to be crazier than I already am :slight_smile:

Tks guys !!!
Yes Joyrider I had made an infinite loop and unity crashed done, I solved this problem.
You were right again, I started too quickly even if I finaly do what I wanted with this scipt, it was not really what I needed …(here the move is not smooth at all :slight_smile: )

I need to animate GameObjects around their center with a limit of 0.1 et -0.1 from their center, if they go far away from that limits, they will not be in the camera field of view.

I know how to smooth a move from a position to an other, but I don’t know how to setup limits for my positions so my planes goes far from the camera.

I’m going to try something.

Try this code, it works. But it s not exactly what I need.
using UnityEngine;
using System.Collections;

    public class Levitation : MonoBehaviour {
    	
    	private GameObject[] Planes;
    	private GameObject[] Groupes;
    	
    	private Vector3[] PosInitiales;
    	private Vector3[] PosInitialesGroup;
    	private Vector3[] PosNew;
    	
    	private float randamFloatX;
    	private float randamFloatY;
    	
    	private bool NewValue;
    	private bool moved ;
    	
    	private float T;
    	private float timeToWait = 1f;
    	private float startTime;
    	
    	
    	void Awake(){
    		moved = false;
    		NewValue = false;
    		Planes = GameObject.FindGameObjectsWithTag ("LevitationObject");
    		Groupes = GameObject.FindGameObjectsWithTag ("LevitationGroupes");
    		
    		PosInitiales = new Vector3[Planes.Length];
    		PosInitialesGroup= new Vector3[Groupes.Length];
    		
    		for (int i = 0; i < Groupes.Length; i++) {
    			PosInitialesGroup _= Groupes*.transform.localPosition;*_

Groupes.transform.localPosition = new Vector3(PosInitialesGroup.x, PosInitialesGroup_.y, PosInitialesGroup*.z);
}
for (int i = 0; i < Planes.Length; i++) {_

PosInitiales _= Planes.transform.localPosition;
Planes.transform.localPosition = new Vector3(PosInitiales.x, PosInitiales.y, PosInitiales.z);
}
StartCoroutine ( WaitAndMove(timeToWait));
}*_

* // Use this for initialization*
* void Start () {*
* randamFloatX = Random.Range(-0.25f, 0.25f);*
* randamFloatY = Random.Range(-0.2f, 0.2f);*

* }*

* // Update is called once per frame*
* void Update () {*
* if(NewValue == true){*
* NewValue = false;*
* StartCoroutine ( WaitAndMove(timeToWait));*
* startTime = Time.time;*
* }*

* T = (Time.time - startTime) / 1f;*

* PosNew = new Vector3[Planes.Length];*
* for (int i = 0; i < Planes.Length; i++) {*

* if( moved == true){*
float lerpX = Mathf.Lerp(Planes_.transform.localPosition.x, PosInitiales*.x, T);
float lerpY = Mathf.Lerp(Planes.transform.localPosition.y, PosInitiales.y, T);*_

Planes_.transform.localPosition = new Vector3(lerpX, lerpY, Planes*.transform.localPosition.z);
}*_

* if(moved == false){*
Planes.transform.localPosition = new Vector3(PosInitiales.x, PosInitiales_.y, Planes*.transform.localPosition.z);
float lerpX = Mathf.Lerp(Planes.transform.localPosition.x, (Planes.transform.localPosition.x+randamFloatX), T);
float lerpY = Mathf.Lerp(Planes.transform.localPosition.y, (Planes.transform.localPosition.y+randamFloatY), T);*_

Planes_.transform.localPosition = new Vector3(lerpX, lerpY, Planes*.transform.localPosition.z);*_

PosNew = Planes*.transform.localPosition;*
* }*
* }*

* if(T >= 1 && moved == true){*
* moved = false;*
* }*
* else if(T >= 1 && moved == false){*
* moved = true;*
* }*

* }*

* IEnumerator WaitAndMove(float waitTime){*
* NewValue = false;*
* yield return new WaitForSeconds(waitTime);*
* randamFloatX = Random.Range(-0.25f, 0.25f);*
* randamFloatY = Random.Range(-0.2f, 0.2f);*
* NewValue = true;*
* }*

}