Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by DVFrance · Aug 06, 2013 at 05:08 PM · animationvector3list

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 :)

Comment
Add comment · Show 5
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 dorpeleg · Aug 07, 2013 at 03:02 PM 0
Share

Is there a reason you want to animate it via script and not via animation?

avatar image DVFrance · Aug 07, 2013 at 03:59 PM 0
Share

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 :)

avatar image dorpeleg · Aug 07, 2013 at 06:27 PM 0
Share

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.

avatar image Joyrider · Aug 07, 2013 at 09:04 PM 0
Share

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 ;)

avatar image DVFrance · Aug 08, 2013 at 09:32 AM 0
Share

Dorpeleg, I can animate them with an animation clip but I will have to create like 100 animations !

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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;
         }
         
     }
     
Comment
Add comment · 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

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

16 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

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


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