- Home /
Recreating scene objects at next level
Hello i have a simple 2d scene with a block that rotates arround self over time.Whenever the player press the mouse click i instantiate another block make it child of the first block save the euler angles of the parent and move the child 2 units away from the parent so the child rotates arround parent.I do this some more times and then i move to the next scene.All the child objects have colliders and if they collide i destroy them.On the next scene i have a script that loads the euler angles that the player clicked and while the parent block rotates with the same speed i want to recreate the objects exactly at the same way.The script that rotates the parent is this.
float[] floats; //the actual ange.z floats stored in the first scene
angle = transform.eulerAngles ;
void Update() {
angle.z += Time.deltaTime * 80;
if (angle.z >= 360)
{
round++; //fire each child every round (every one full rotation)
attackedThisRound = false;
angle.z = 0;
}
transform.eulerAngles = angle;
if (round < floats.Length && !attackedThisRound && angle.z >= floats[round] )
{
InstantiateChild();
attackedThisRound = true;
Debug.Log("Desired :" + floats[round] + " Actual:" + transform.eulerAngles.z);
}
}
Now What is happening.Lets say i fire a block when the Parent's ange.z is at 45.565f. At the next level it will be fired between 44.5-47.5f and the child may land on another child and destroyed.Now this when tested in different pcs with different frame rates will have a lot worse results,on mobile even worse. These are some logs from the unity editor.Testing on my devices i had ~5f max error. Desired :209.7344 Actual:209.5964 Desired :253.9744 Actual:254.9251 Desired :291.1291 Actual:291.6958 Desired :326.7104 Actual:326.6832 Desired :1.343259 Actual:1.261122 Desired :142.5259 Actual:143.0578 Any ideas?
I tried setting the physics time.fixedTime to 0.01 and move above code to fixed update so every FixedUpdate i rotate the cube by 0.8. and then i check if angle.z >= floats[round]-0.4f so the max error will be 0.4 degrees. Am i right?Will the 0.01 fixed time step affect my game performance?Is there any other way that i m missing?
I am so confused. Setting fixed time step to 0.01 had a lot better result in unity editor,almost acceptable and 1800 fps. But building and exporting to my android device(lg g3) there was a constant lag all over the scene.I use only 1 parent , 10 child sprites and one script.Nothing more. Edit: It seems like Android devices have Vsync enabled.When i enabled it at Quality settings ,editor had lag too!
Answer by DiegoSLTS · Oct 14, 2015 at 02:26 PM
I'm not sure why you're changing the scene if you want things to keep moving, it's like you're setting up a hard problem to solve that could be avoided.
Anyway, you're doing physic stuff in the Update function, that's why you get different results with different framerates. Use FixedUpdate instead. But note that Unity's physic simulation is NOT deterministic, it's not ensured that the same objects in the same setup will move always the same way. Ofcourse they follow the same laws, but a lot of things can alter the result.
Also, have you tried setting anything you want to keep from one scene as DontDestroyOnLoad? Unity will keep those objects in the scene when loading a new one, but I'm not sure how the current physic state is handled (I guess it's not altered, but physics are complicated, and loading a new scene messes with time and framerate for a few milliseconds).
Ok you are right thanks. As i mentioned above i am using FixedUpdate ins$$anonymous$$d of update now and i think its working ok now. I forgot to change Deltatime to FixedDeltatime and the results were messed up. Anyway still i dont understand why the game lags in my android device.I used an FpsDisplay script and the game has always 60 fps but the image is jerky/choppy when the objects rotate.I ve tested in bluestacks and jennymotion and they still lag at constant 60 fps.
I don't know about that, maybe your FpsDisplay script is not working O$$anonymous$$? $$anonymous$$aybe you should ask a new question for that, it doesn't sound related to this question.
This is the script i am using link text . Well maybe you are right i should start a different question regarding the frame rate.Thanks for your time!