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 Physics.Simulate; Inaccurate Trajectory Prediction

Discussion in 'Scripting' started by UnityGuy1988, Sep 14, 2023.

  1. UnityGuy1988

    UnityGuy1988

    Joined:
    Jan 12, 2020
    Posts:
    24
    I'm trying to make a predicted trajectory line for a ball using a separate 'simulation' scene and Physics.Simulate to plot each point of a simulated ball instance with line renderer. The problem is, the predicted trajectory falls way short of the flight of the actual ball. I'm wondering whether it's because my projection line code is running from Update (or FixedUpdate, tried them both!), while the actual ball itself is triggered by the OnCollisionEnter method. Both the simulation ball and the actual ball are identical in their physics properties, I've checked that too. Can anyone give me an insight as to how the OnCollisionEnter method works, seeing as it doesn't have to be called from either Update or FixedUpdate?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    You'd have to show your code. Properly done, it should work fine.
     
    Bunny83 likes this.
  3. UnityGuy1988

    UnityGuy1988

    Joined:
    Jan 12, 2020
    Posts:
    24
    The whole script's a bit of a mess right now. I've been playing around with a lot of things, trying to get it to play how I would like it. I'll tidy it up a bit and then post it for you.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,507
    The main thing to know is that it's not just about calling the manual simulation but also calling it in steps of whatever the "real" simulation runs at i.e. time-integrating the simulation. Typically this is lots of "Time.fixedDeltaTime" steps.

    Also, calling simulate does not called FixedUpdate, it's the other way around; the time manager calls FixedUpdate which calls the scripts, animation, physics etc. Nothing you do in your scripts will be called by this.

    Beyond that, the automatic simulation that Unity performs calls the exact same thing i.e. Physics.Simulate so it's not a different code-path or anything. Same for 2D BTW.

    I don't follow what the question means. Callbacks are called at the end of the simulation step no matter whether you manually simulate or Unity itself just calls it for you. These in-fact are the only things in scripts that get called because it's the physics simulation step calling them.
     
    Bunny83 likes this.
  5. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    I would verify that's correct. As physics are just a mathematical calculation, so if "all" the same variables(scale, mass, drag, force applied, angular drag, etc...) are being equated, then the result should be the same. Unless real life, then air temp and wind speed do kinda mess with you, lol..

    OnCollisionEnter is basically a "message" that get's broadcasted, so it's "update" get's run from further up the line. But seeing as though you question this, this makes me wonder if your problem isn't in that area?

    But either way, we'd need to see some code snippets, to get a better idea what is going on. I suggest "NotePad++" as a off-hand code editor, that way you can copy/paste(and clean) and paste here without showing your nightmare of spaghettis(it's what I do). ;)
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,507
    Mathematical yes but this is numerical integration so the time-step you specify in the simulate call is just as, if not more, important than anything.

    I only mentioned this because so many times have I seen devs simulating large times, even in steps and then comparing it to the 50Hz time-step (default) used.
     
    wideeyenow_unity likes this.