Home > other >  A friend introduced BBS.. Questions about the Unity3D ~ ~ for help
A friend introduced BBS.. Questions about the Unity3D ~ ~ for help

Time:09-25

I'm going to do with Unity3d a visual bubble sort. C # write a bubble is very simple, but in the Unity3d is not the same thing,,, ask everybody to help me take a look at the code,
Don't know what is going wrong code below

Void the Update ()
{
For (I=0; i {

For (j=I + 1; J & lt; Shapes. Length; J++)
{
X0=shapes [I]. Transform. Position. X.//extraction 2 cube location information
Y0=shapes [I]. Transform. Position. Y;
Z0=shapes [I]. The transform. The position, z;
X1=shapes [j]. The transform position. X;
Y1=shapes [j]. The transform position. Y;
Z1=shapes [j]. The transform position. Z;

Isize=shapes [I]. Transform. LocalScale. Y;//extraction 2 cube length, which is used to compare the size
Jsize=shapes [j]. The transform localScale. Y;

If (isize & gt; Jsize)
{
Shapes [j]. The transform. The position=new Vector3 (Mathf. Lerp (shapes [j]. The transform. The position, x, shapes [I] the transform. The position, x, speed * Time deltaTime), shapes [1]. The transform. The position. The y, shapes [1]. The transform. The position, z);
Shapes [I]. The transform. The position=new Vector3 (Mathf. Lerp (shapes [I] the transform. The position, x, shapes [j]. The transform. The position, x, speed * Time deltaTime), shapes [0]. The transform. The position. The y, shapes [0]. The transform. The position, z);
//swap places
}
}
}

}

CodePudding user response:



After the start of middle fit,,

CodePudding user response:

Did you solve?

CodePudding user response:

No, I'm going to overnight programming

Call this update should be hundreds of times per second, and a for loop is circulating hundreds of times per second, but helpless pain in the inside of the for translational operation could finish?
Seems to involve coroutines what of, I really can't do anything,, late at night I also will come to see ~ ~ ~

CodePudding user response:

 
GameObject [] shapes;
Float [] shapes_x;
//Use this for initialization
Void the Start ()
{
Shapes=new GameObject [6].
Shapes_x=new float [6].
Shapes [0]=GameObject. Find (" Cube ");
Shapes [1]=GameObject. Find (" Cube1 ");
Shapes [2]=GameObject. Find (" Cube2 ");
Shapes [3]=GameObject. Find (" Cube3 ");
Shapes [4]=GameObject. Find (" Cube4 ");
Shapes [5]=GameObject. Find (" Cube5 ");
}

Bool flag=false;
//Update is called once per frame
Void the Update ()
{
If (flag==false)//sort said it had to come over, or it will cause has been sorting, shine
{
for (int j=0; J & lt; Shapes. Length; J++)
{
For (int I=shapes. The Length - 1; I & gt; j; I -)
{
Float x0=shapes [I]. Transform. Position. X.//extraction 2 cube location information
Float y0=shapes [I]. Transform. Position. Y;
Float z0=shapes [I]. The transform. The position, z;
Float x1=shapes [j]. The transform position. X;
Float y1=shapes [j]. The transform position. Y;
Float z1=shapes [j]. The transform position. Z;

Float isize=shapes [I]. Transform. LocalScale. Y;//extraction 2 cube length, which is used to compare the size
Float jsize=shapes [j]. The transform localScale. Y;

If (isize & gt; Jsize)
{
GameObject tem;
Tem=shapes [I];
Shapes [I]=shapes [j];
Shapes [j]=tem;
}
}
}

For (int k=0; K & lt; Shapes. Length; K++)
{
Shapes_x [k]=shapes [k]. Transform position. X;
}
System. Array. Sort (shapes_x);

for (int j=0; J & lt; Shapes. Length; J++)
{
for (int i=0; i {
If (j==I)
{
Shapes [j]. The transform. The position=new Vector3 (Mathf. Lerp (shapes [j]. The transform. The position, x, shapes_x [I], 50 f * Time deltaTime), shapes [j]. The transform. The position. The y, shapes [j]. The transform. The position, z);
}
}
}

flag=true;
}
}
  • Related