Search Unity

Question Local 2D and 3D physics scenes with Physics2DRaycaster

Discussion in 'Physics' started by OmarOfMA, May 22, 2023.

  1. OmarOfMA

    OmarOfMA

    Joined:
    Sep 6, 2022
    Posts:
    13
    Hello,

    I have a 2D game with 2 scenes.
    I want to keep their physics independent, but I'm facing a few issues.

    I am loading the scenes like this:
    Code (CSharp):
    1. LoadSceneParameters sceneParameters = new LoadSceneParameters(LoadSceneMode.Additive, physics);
    2. SceneManager.LoadSceneAsync(scene.Path, sceneParameters);
    Then I called Simulate in FixedUpdate:
    Code (CSharp):
    1. var scene = gameObject.scene;
    2. var physics3D = scene.GetPhysicsScene();
    3. physics3D.Simulate(Time.fixedDeltaTime);
    4. var physics2D = scene.GetPhysicsScene2D();
    5. physics2D.Simulate(Time.fixedDeltaTime);
    The issues:
    1- I use Physics2DRaycaster, but it seems like all the interactions that depend on it no longer work with a local physics scene.
    2- I have both 2D and 3D physics in my scenes, but I'm unable to make them both work at the same time.

    I tried setting physics to LocalPhysicsMode.Physics2D, LocalPhysicsMode.Physics3D, and LocalPhysicsMode.Physics2D | LocalPhysicsMode.Physics3D.
     
  2. OmarOfMA

    OmarOfMA

    Joined:
    Sep 6, 2022
    Posts:
    13
    Update:
    I made a more minimal test. It's actually possible to give a scene its own local 2D and 3D physics at the same time.
    So I must have a more specific issue with my setup.

    I still have an issue with Physics2DRaycaster and IPointerClickHandler though. It doesn't seem to work when a scene is loaded additively and given its own local physics.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,474
    Likely it all uses the default physics scene.

    So you know, Physics2DRaycaster, is not a physics feature implemented by any physics team, it's a UGUI event system feature. IPointerClickHandler is also an event-system thing.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,474
  5. OmarOfMA

    OmarOfMA

    Joined:
    Sep 6, 2022
    Posts:
    13
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,474
    Personally if you want to do physics work, I'd just use the physics queries directly then you have full control.

    These other systems are always only going to give you the most common basic behaviour.
     
    OmarOfMA likes this.
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,474
    Just to add, whilst GetRayIntersection is a nice pseudo ray-based query, if all you want is to find what overlaps the cursor then you should use OverlapPoint.
     
    OmarOfMA likes this.