Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Is Unity random?

Discussion in 'Unity Hub' started by PanRafal, Aug 10, 2023.

  1. PanRafal

    PanRafal

    Joined:
    Jun 6, 2023
    Posts:
    10
    Im currently learning unity and im on the junior programming path. I made a scene with a car hitting a box. First of all, behaviour of the box doesn't look realistic at all and also doesn't behave like in the tutorial video. Second, I noticed that behaviour of the box is random. While changing absolutly nothing, box sometimes acts differently. What the hell is going on here? Is unity physics randomized for some reason? Is it bugged? I use 2021.3.26f1 version. Can anyone explain this?
     
  2. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    10,977
    The term you are looking for is determinism.

    But how are you repeating the box collision? is the car moving the exact same way and are the exact same forces applied (which is not easily done if you don't explicitly prepare it to behave that way).
     
  3. PanRafal

    PanRafal

    Joined:
    Jun 6, 2023
    Posts:
    10
    This is the excercise I was doing: https://learn.unity.com/tutorial/1-...0200e9de0&projectId=5caccdfbedbc2a3cef0efe63#
    Its a very simple scene, there is only, a road, a car, a box and some fake enviorement. I did everything like they said and I get different result, which is also random. The only thing im doing is pushing play button. There is no wind, there arent even any added forces. Its completely basic stuff, which should always work the same.

    EDIT: Noticed more weird things, when I set vehicle speed to 50 (units per second I guess) it went through the box just slightly moving it and when I changed box weight to 200kg the vehicle once flew up over it like it was a ramp and box didnt move at all, and now it just makes it jump up like the change of mass didnt change anything.
     
    Last edited: Aug 11, 2023
  4. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,833
    Floating point calculations on all Unity platforms should be deterministic; that is, IEEE754 is the standard for floating point calculations, and all Unity-supported platforms rely on this monumental standard.

    One problem with Unity and determinism is that the physic engine is a forward integrator (calculates the next state from the current state and a small timestep) with a hard-to-control timestep. If you run with a constant
    Time.fixedDeltaTime
    and never do any calculations outside of FixedUpdate() methods, it should be pretty darn close to deterministic behavior on any machine with the same value for the timestep.

    But it's really easy to have variations in
    Time.deltaTime
    (as opposed to
    Time.fixedDeltaTime
    ) and anything dependent on that can throw off the number of frames or the distances moved in the physics portion. The learning video implements a forward motion in the Update() method, which by default is run as fast as your computer allows, depending on processor speed and graphics pipeline performance. Since the Update cycle depends on Time.deltaTime, and Time.deltaTime varies, you will definitely have determinism problems. The code sample also moves the car using the transform.position variable, which is simple to demonstrate here but causes additional determinism issues for the Unity physics system.

    You could try to lock this all down, but take this lesson for what it is: a high level introduction to how scripts and the Unity game objects interact.
     
  5. PanRafal

    PanRafal

    Joined:
    Jun 6, 2023
    Posts:
    10
    Thanks, but tell me why physics in the tutorial video work different than in my editor? In the video we can see that box is pushed by the car as it should be, in my editor the car rides under the box like it was much smaller than the box, and the gravity works weird, like the mass of the box didnt matter much and it was made of paper.