In my 3d game,I need to input number via inputfield and create the same number of cubes as I input. Then I need to transform the cubes I created and make a small keyframe animation. How can I do that?
Everything is possible through script. Just gotta know how. I haven’t done a much of this with animation because i use iTween asset which is free and easier to make easing in the curves. but i would start here Unity - Scripting API: AnimationCurve.AnimationCurve
i wrote a super simple example for you. this just changes postions at points but you could allways repeat this process for rotations as well. Anyways, attach this to a new empty game object:
public Vector3[][]anim;
int i;
int i2;
int i3;
Transform[] parts;
float timer;
int frame;
float[][]speeds;
void Start(){
i = 3;
anim = new Vector3*[];*
_ while(i>0){i–;anim*=new Vector3[3];}*_
* //assign whatever animations points to our three parts*
* anim [0] [0] = new Vector3 (2,1,1);*
* anim [0] [1] = new Vector3 (-2,-1,-1);*
* anim [0] [2] = new Vector3 (1, 0, 0);*
* anim [1] [0] = new Vector3 (0, 0, 3);*
* anim [1] [1] = new Vector3 (0, 0, 0);*
* anim [1] [2] = new Vector3 (2, 0,-3);*
* anim [2] [0] = new Vector3 (0, 1, 0);*
* anim [2] [1] = new Vector3 (0, 0, 0);*
* anim [2] [2] = new Vector3 (1,-2, 0);*
* i = anim.Length;*
_ speeds = new float*;*_
while (i>0) {i–;
i2=anim*.Length;*
_ speeds*=new float[i2];
//-----calculate speeds between every point with distance*
* while(i2>0){i2–;
i3=i2-1;
if(i3<0){i3=anim.Length-1;}
speeds[i2]=Vector3.Distance(
anim[i2],
anim[i3]);}}
//-------make some cubes*
* parts=new Transform[3];
i = parts.Length;
while (i>0) { i–;
parts = GameObject.CreatePrimitive (PrimitiveType.Cube).transform;
parts .parent = transform;
parts .localPosition = anim [0];}}
void Update(){
//------------animate our parts*
* i = parts.Length;
while(i>0){i–;
parts.localPosition=Vector3.MoveTowards(
parts.localPosition,
anim[frame],speeds[frame]Time.deltaTime);
if(i==0){
if(parts.localPosition==anim*[frame]){frame++;
if(frame>anim.Length-1){frame=0;}}}
}
}*_