Search code examples
unity-game-enginephysicsraycasting

How to use Raycast in an additive scene with local physics?


Can Raycast really only be used on the main scene?

If I load an additive scene with an indication of the local physics for the scene, then the methods of the Physics on the loaded scene are not executed (that is, for example, rays do not register a hit).
However, triggers, collisions work.

`var parameters = new LoadSceneParameters(LoadSceneMode.Additive, LocalPhysicsMode.Physics3D);

Scene gameScene = SceneManager.LoadScene("Island", parameters);`



If I do not specify LocalPhysicsMode.Physics3D in the parameters, everything works as it should.

On an additive scene, I simulate physics using Simulate (). Everything works, except for the methods of the Physics class.

I loaded an additional scene and specified the local physics for this scene in the parameters. But when I use raycast on this scene, it doesn't respond to hits at all.


Solution

  • When using separate physics scenes you can not use the normal global Physics.Raycast.

    Instead you have to go through the scene you loaded and use th extension method GetPhysicsScene

    var physicsScene = gameScene.GetPhysicsScene();
    

    store it somewhere and then go through PhysicsScene.Raycast the way you would usually use the normal one

    if(physicsScene.Raycast(.....))
    {
        // do something
    }