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();
}
}
'''