#Physics.IgnoreCollision in Netcode for GO

1 messages · Page 1 of 1 (latest)

sacred yoke
#

I ignore why the instruction Physics.IgnoreCollision here does not work. It has the purpose of avoiding that the spell (which is a simple sphere with only the script 'Spell' added to it) is destroyed because as soon as it is instanciated it collides with the player that has just instantiated it. The script 'CastTheSpell' is added to the player. I know I could have avoided this issue simply instantiating the spell outside the player but I want to understand why Physics.IgnoreCollision here does not work.The example is very simple: just two classes:

'''
using Unity.Netcode;
using UnityEngine;
using UnityEngine.InputSystem;

public class CastTheSpell : NetworkBehaviour
{
[SerializeField] GameObject spell;

void Update()
{
    if (!IsOwner) return;

    if (Keyboard.current.spaceKey.wasPressedThisFrame)
    {
        CastTheSpellServerRpc();
    }
}

[ServerRpc]
void CastTheSpellServerRpc()
{
    GameObject newSpell = Instantiate(spell, transform.position + Vector3.up +
                                     Vector3.forward * 0.35f, transform.rotation);
    Physics.IgnoreCollision(newSpell.GetComponent<SphereCollider>(),  
                                          GetComponent<CapsuleCollider>(), false);
    newSpell.GetComponent<NetworkObject>().Spawn(true);
    newSpell.GetComponent<Rigidbody>().AddForce(Vector3.forward * 15f,
                                                               ForceMode.Impulse);
}

}

using Unity.Netcode;
using UnityEngine;

public class Spell : NetworkBehaviour
{

void OnCollisionEnter(Collision collision)
{
    Debug.Log(collision.gameObject.name);
    if (!IsOwner) return;

    DespawnServerRpc();
}

[ServerRpc]
void DespawnServerRpc()
{
    NetworkObject.Despawn();
}

}
'''

tough mist
#

Seems like you're only running Physics.IgnoreCollision on the server

sacred yoke
tough mist
#

You could try something like calling it in OnNetworkPreSpawn in Spell provided you're using a version of NGO with that function.

#

But it would probably be simpler to just use layers for the collision ignoring, and call it in Awake or Start in a script that runs on all clients + the server

sacred yoke
tough mist
#

which means you can't ignore it until it is spawned on the clients

#

and by the time it is spawned on your clients, it will collide with your player

#

which is the issue you're having

tough mist
#

if you chose not to use layers