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

physicsSim.Simulate broken

Discussion in 'Physics' started by eron82, May 2, 2020.

  1. eron82

    eron82

    Joined:
    Mar 10, 2017
    Posts:
    83
    I have a simple scene with a ball with a random speed and a script with physicsSim.Simulate(Time.fixedDeltaTime) to know where the ball will bounce on the ground, then a new random speed is applied and new simulated bounce are calculated and so on. Randomly the simulated ball is stucked onto the ground at the first bounce, but the real ball bounces more the 2 times. This doesn't happen if the initial speed is not random but it's always the same.
     
  2. eron82

    eron82

    Joined:
    Mar 10, 2017
    Posts:
    83
    I noticed that if i change the default contact offset from 0.01 to 0.1 or another value the bug always happens. The simulated ball touches the ground the first time and it doesn't bounce. The real ball bounces normally.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,533
    Simulation isn't broken despite the title. This specifc call isn't a code path only you can call, it's what is called internally when you're not manually simulating i.e. by Unity so it's code that's executed by default for everyone using physics.

    You don't specify if this is 2D or 3D physics (I'll assume 3D) but not bouncing is either the restitution on a material not set as expected on both colliders, it's the Physics.bounceThreshold or something in your movement code.
     
  4. eron82

    eron82

    Joined:
    Mar 10, 2017
    Posts:
    83
    Yes it's for 3D physics. I noticed that sometimes the OnCollisionEnter is called only 1 time and sometimes is called 6 times on the same collision.
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,533
    Maybe if you described your bug, your set-up, expectations etc then it might be clear what's going wrong. Beyond that, these observations don't really narrow anything down.

    Note that I already provided some info above.
     
  6. eron82

    eron82

    Joined:
    Mar 10, 2017
    Posts:
    83
    The script is simple, when the simulated ball touches the ground the second time the simulation is repeated, so there is a new toss of the ball and a new simulation. I have this script to check the bounce:

    private void OnCollisionEnter(Collision collision)
    {
    bouncenum = bouncenum + 1;

    if (bouncenum == 1)
    {
    Debug.Log( collision.transform.position);
    }

    if (bouncenum == 2)
    {
    Debug.Log( collision.transform.position)
    }
    }

    Sometimes there is no log for the second bounce, maybe because sometimes there are more than one OnCollisionEnter calls at the first bounce
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,533
    Yes, you've already stated you get multiple "Enter" callbacks so seeing how you report them doesn't really resolve anything. You'll get multiple Enter/Exit. As for why your setup is doing what it is I have no idea because I have little info to go on. I could guess but I don't want to get into a back/forth guessing game with limited info.

    You've still not mentioned if you've tried what I stated above such as the bounce-threshold.

    If nothing else, show a video or the configuration of what is bouncing etc. Make sure you don't have gravity set to some crazy high value.

    Note: You can use code-tags for posting code.
     
    Last edited: May 3, 2020
  8. eron82

    eron82

    Joined:
    Mar 10, 2017
    Posts:
    83
    ok, if i change the line:

    if (bouncenum == 2)

    into

    if (bouncenum >= 2)

    i have no crash because the simulation is repeated only after the second log. Is it normal that there are multiple OnCollisionEnter callbacks with a single bounce?
     
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,533
    Crash? What crash?

    Sorry but you're not responding to anything I'm asking so until you do I'm not sure how I can help you further.
     
  10. eron82

    eron82

    Joined:
    Mar 10, 2017
    Posts:
    83
    Sorry but i see no question in your posts
     
  11. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,533
    You have to understand that I don't have your project in front of me and cannot provide you with answers if I don't understand what you're doing. Know how you report multiple Enter isn't useful and then adding in about some crash just makes things even more confusing.
     
  12. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,533
    btw, if you want to host a smiple reproduction then I'd be happy to take a look at it. Alternately, if you DM me I can send you a link where you can securely upload it.
     
  13. eron82

    eron82

    Joined:
    Mar 10, 2017
    Posts:
    83
    Ok i will try to post a project.
     
    MelvMay likes this.