#Collisions happening between objects in different additive scenes

8 messages · Page 1 of 1 (latest)

inland dust
#

Hey there,

I'm having an issue where clients can correctly only see objects in the additive scene they are in, but are able to take damage from collisions with objects from other scenes.

I have three scenes:

  1. Main - My offline scene that basically just has a 'find match' button that calls StartClient()
  2. Queue - An empty container scene that is set to the online scene on the network manager
  3. Arena - the additive scene players are moved two when two people connect to the server / enter the queue.

I've added a SceneInterestManagement to my Network Manager in the main scene and added a PhysicsSimulator to an empty GameObject in the Arena scene.

When players connect, they are added into lobbies and when the lobby reaches max players (2), this StartMatch function is called in my custom network manager

private IEnumerator StartMatch(Lobby lobby)  
{
    var op = SceneManager.LoadSceneAsync("Arena", LoadSceneMode.Additive);  
    yield return op; // Wait for the scene to load  
  
    lobby.Scene = SceneManager.GetSceneAt(SceneManager.sceneCount - 1);  
  
    if (lobby.Scene.IsValid())  
    {        
        foreach (var conn in lobby.Players)  
        {
        // Destroy old player object  
            if (conn.identity != null)  
            {
                NetworkServer.Destroy(conn.identity.gameObject);  
                yield return new WaitForEndOfFrame();  
            }
            
            // Send message to client to load the new scene  
            conn.Send(new SceneMessage { sceneName = lobby.Scene.name, sceneOperation = SceneOperation.LoadAdditive });  
            yield return new WaitForEndOfFrame();
            
            base.OnServerAddPlayer(conn);
            SceneManager.MoveGameObjectToScene(conn.identity.gameObject, lobby.Scene);  
        }
    }
    else  
    {  
        Debug.LogError("Failed to load Arena scene.");  
    }
}

Is there something obviously wrong about how I'm moving the players into a new Arena scene here?

If I look at the scene from the server's perspective (image attached) when I have 4 players that have queued up, it all looks good. The players are separated into different scenes, but the fireball object at the bottom of the first arena scene did damage to a player in the second scene.

The Fireball in this case is spawned via a [Command] attributed function, the code that actually spawns the object is here:

public override void Cast(GameObject caster, Vector2 startPosition, Vector2 targetPosition)  
{  
    if (!NetworkServer.active) return;  
  
    var projectile = Instantiate(projectilePrefab, startPosition, Quaternion.identity);  

    SceneManager.MoveGameObjectToScene(projectile, caster.scene);  
    NetworkServer.Spawn(projectile);  
    
    projectile.GetComponent<Projectile>().SetTarget(targetPosition);  
}

Any help in understanding why the server-handled collisions aren't recognizing the different scenes would be greatly appreciated, thanks!

sterile cargo
#

To test if collision is working or not, I did is the following, once a game starts on the server side, I instantiate the TestUnit Prefab.

 // TODO: remove test
 var testUnitInstance = Instantiate(GameManager.Instance.GetTestUnit());

 testUnitInstance.name = $"TestUnit [id={playerid}]";

 SceneManager.MoveGameObjectToScene(testUnitInstance, newScene);
 // I simply place the TestUnit opposite of each other to make it simple for projectile to hit other player 
 testUnitInstance.transform.position = new Vector3(lobbyPlayerData.playerLobbyIndex % 2 == 0 ? 10 : -10, 0.5f, 0);

 NetworkServer.Spawn(testUnitInstance, lobbyPlayerData.isBot ? null : conn);

 if (testUnitInstance.TryGetComponent(out ColorSetter testUnitColorSetter))
 {
     testUnitColorSetter.SetColor(lobbyPlayerData.playerColor);
 }

Now I've attached TestHealth.cs script to the TestUnit Prefab https://pastie.io/orgnfn.cs
And I've created a Projectile Prefab and attached TestProjectile.cs script to it https://pastie.io/qmexcz.cs

and the result was as shown in the below video, collisions works just fine.

wise aurora
wise aurora
inland dust
#

Wow yeah it was just because I was missing the localPhysicsMode.. That's slightly embarrassing, thanks for the replies, have marked as resolved

#

Also you shouldn't be networking projectiles
It seems to be working ok for now, they're fairly slow moving projectiles. Are there any examples you'd recommend for projectiles that aren't networked?

wise aurora