- Home /
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[i] = Planes[i].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[i]){
PlanesToMove.transform.localPosition = PosInitiales[i];
}
if(PosNew[ib] == PosInitiales[i]){
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 :)
Is there a reason you want to animate it via script and not via animation?
Because there is a lot of objects, like 50 for this project and all of them don't have the same center and they don't have the same parents too.
And because I'm trying to improve my skills in scripting :)
I'm not sure why it's not moving for you (it's a little too much code to wrap my head around right now).
But why don't you give a try to AnimationCurve?
It can create animation at run time and might be easier to work with.
I don't know what you're trying to do exactly... but you're moving every plane like 100 times every frame...
your foreach goes through all your planes already, but then for every plane you have a for loop that goes through them again, and in that loop you have another for loop that is going through them yet again... And it seems you're resetting the positions within that loop every time they are not at the initial position...
If I were you, I'd delete what I have in that foreach, and calmly think what it is exactly that you want to do. How do you want them to move, what are the limits, I'm guessing the movement should be smooth (so no resetting to initial position, but move towards it)... And maybe we cal help you figure out a way to do that ;)
Dorpeleg, I can animate them with an animation clip but I will have to create like 100 animations !
Answer by DVFrance · Aug 08, 2013 at 07:54 AM
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 :) )
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[i] = Groupes[i].transform.localPosition;
Groupes[i].transform.localPosition = new Vector3(PosInitialesGroup[i].x, PosInitialesGroup[i].y, PosInitialesGroup[i].z);
}
for (int i = 0; i < Planes.Length; i++) {
PosInitiales[i] = Planes[i].transform.localPosition;
Planes[i].transform.localPosition = new Vector3(PosInitiales[i].x, PosInitiales[i].y, PosInitiales[i].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[i].transform.localPosition.x, PosInitiales[i].x, T);
float lerpY = Mathf.Lerp(Planes[i].transform.localPosition.y, PosInitiales[i].y, T);
Planes[i].transform.localPosition = new Vector3(lerpX, lerpY, Planes[i].transform.localPosition.z);
}
if(moved == false){
Planes[i].transform.localPosition = new Vector3(PosInitiales[i].x, PosInitiales[i].y, Planes[i].transform.localPosition.z);
float lerpX = Mathf.Lerp(Planes[i].transform.localPosition.x, (Planes[i].transform.localPosition.x+randamFloatX), T);
float lerpY = Mathf.Lerp(Planes[i].transform.localPosition.y, (Planes[i].transform.localPosition.y+randamFloatY), T);
Planes[i].transform.localPosition = new Vector3(lerpX, lerpY, Planes[i].transform.localPosition.z);
PosNew[i] = Planes[i].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;
}
}
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
pendulum simulation with force vectors 2 Answers
instantiate problem help? 1 Answer
Animation spins wildly after completed 0 Answers