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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

question about manually updating physics

Discussion in 'Physics' started by EliJNemer, May 20, 2020.

  1. EliJNemer

    EliJNemer

    Joined:
    Aug 3, 2019
    Posts:
    123
    hello,

    if i want to update physics manually i have to call physics.Simulate(time.Fixeddeltatime);

    but this needs to be called in update. which is called 60 times a second..

    if my update tick was 0.01 so i would want 100 ticks a second for physics, then in this case it would tick 60 times giving me 0.60 of a second..

    how is this gap reconciled between the autoSimulate and Manual?
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    If you want to update physics per-frame then use the per-frame time Time.deltaTime if you want the physics simulation to stay in-sync with game time.

    If you want what happened previously then call it during the FixedUpdate with the Time.fixedDeltaTime.
     
    EliJNemer likes this.
  3. EliJNemer

    EliJNemer

    Joined:
    Aug 3, 2019
    Posts:
    123
    Thank you
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    No problem. Note that changes to the step interval of the simulation won't give you identical results because of the numerical integration involved. Also, per-frame is a variable frame-rate unless obviously you're hardware linked to VSync so can give you inconsistent results.
     
    EliJNemer likes this.
  5. EliJNemer

    EliJNemer

    Joined:
    Aug 3, 2019
    Posts:
    123
    @MelvMay

    so i wrote some physics for my game, i am pretty much only using the code i wrote for velocity resolution and velocity after contact with another object. i am using unity's collision detection though.
    this is fine till now, but i have a script which simulates 100 time.FixeddeltaTime into the future, by calling the function.

    simulate() in update

    void simulate()
    {
    for(int i = 0, i < 100; i++)
    {
    //here i might add some code because i dont think that the code i wrote for velocity resolution is called by This function so i add it here to be sure that it is called..

    Physics.Simulate(time.FixeddeltaTime);
    }
    }

    but it does not work, ie. when i take the shot the simulated path is longer than the actual shot path.. i debugged oncollisionEnter and it is being called during the simulation.. so it IS being called..

    but it does not seem to be having an effect on the simulation..

    is there something im doing wrong here or missing or is it working and im just not using it right?
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    Do you know that "Physics.Simulate()" runs the main global simulation and NOT a separate local physics scene? You say some "actual shot path" but I have no idea what physics scene that is. If you have separate simulation scenes then you should simulate them separately. Also, you don't ellaborate on this discrepancy. You simulation is 100 steps so you're saying you do 100 steps in the "actual shot path" and it's different which I don't understand.

    I really don't have enough information. It seems you might be misunderstanding what it does.

    In the end though, I'm not a 3D physics dev but this doesn't sound like a 3D specific issue but I'm just not sure.
     
    Last edited: May 27, 2020