Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question 2d physics callback not triggered when run in editor time

Discussion in 'Physics' started by UNCLEPOW, Jul 4, 2023.

  1. UNCLEPOW

    UNCLEPOW

    Joined:
    Apr 26, 2019
    Posts:
    4
    I am doing some physics tests in the editor time, but i met some problems that 2d physics callbacks(OnCollisionEnter2D & OnTriggerEnter2D) are not triggered properly. Here are the main codes.

    This is a simple editor window to simulate physics in editor time. Hit the ‘Run Physics’ button to step physics.
    Code (CSharp):
    1. public class ScenePhysicsTool : EditorWindow
    2. {
    3.    private int times = 1;
    4.    private float timestep = 0.01f;
    5.    private GameObject obj = null;
    6.    private void OnGUI()
    7.    {
    8.        times = EditorGUILayout.IntField(times);
    9.        timestep = EditorGUILayout.FloatField(timestep);
    10.        obj = EditorGUILayout.ObjectField(obj, typeof(GameObject), true) as GameObject;
    11.        if (GUILayout.Button("Init"))
    12.        {
    13.            Physics2D.simulationMode = SimulationMode2D.Script;
    14.            Physics.autoSimulation = false;
    15.        }
    16.        if (GUILayout.Button("Run Physics"))
    17.        {
    18.            StepPhysics();
    19.        }
    20.        if (GUILayout.Button("Reset"))
    21.        {
    22.            obj.GetComponent<Rigidbody2D>().velocity = Vector2.zero;
    23.        }
    24.    }
    25.  
    26.    private void StepPhysics()
    27.    {
    28.        for (int i = 0; i < times; i++)
    29.        {
    30.            //2d
    31.            Physics2D.Simulate(timestep);
    32.  
    33.            //3d
    34.            Physics.Simulate(timestep);
    35.        }
    36.    }
    37.  
    38.    private void Reset()
    39.    {
    40.        this.obj.GetComponent<Rigidbody2D>().velocity = Vector2.zero;
    41.        this.obj.transform.position = new Vector3(0, 5, 0);
    42.    }
    43.  
    44.    [MenuItem("Tools/Scene Physics")]
    45.    private static void OpenWindow()
    46.    {
    47.        GetWindow<ScenePhysicsTool>(false, "Physics", true);
    48.    }
    49. }
    This is the trigger script that is attached to the gameobjects.
    Code (CSharp):
    1. [ExecuteInEditMode]
    2. public class Trigger : MonoBehaviour
    3. {
    4.    private void OnCollisionEnter2D(Collision2D collision)
    5.    {
    6.        Debug.Log("OnCollisionEnter2D");
    7.    }
    8.  
    9.    private void OnTriggerEnter2D(Collider2D collision)
    10.    {
    11.        Debug.Log("OnTriggerEnter2D");
    12.    }
    13.  
    14.  
    15.    private void OnTriggerEnter(Collider other)
    16.    {
    17.        Debug.Log("OnTriggerEnter");
    18.    }
    19.  
    20.  
    21.    private void OnCollisionEnter(Collision collision)
    22.    {
    23.        Debug.Log("OnCollisionEnter");
    24.    }
    25. }
    This is the final log result. It’s obvious that only the 3d physics callback is triggered just as I expected.

     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,101
    Just a FYI: there's no major code-path difference between you calling the simulation call or Unity automatically calling it. It's physically impossible for it to not be called. In other words, using Script mode is no different than FixedUpdate or Update mode. The main difference here though is play-mode. We only produce those callbacks in play mode in 2D. I wasn't aware that 3D produced them outside of it TBH. The problem with doing that is if you have delete calls (etc) in them, you'll loose those objects being deleted in edit mode.

    It's entirely possible to allow this though and it'd clearly be good to have this as a "feature" parity issue so it can be added but for 2D I'd implement it as an option in the project settings.

    If you feel this is something you'd like to see then I'd ask that you please report this a "bug" and provide me with the case ID so I can look into it.

    Thanks for bringing this to my attention.