- Home /
Setting IgnoreCollision on Network spawned objects. (Unet)
I'm using the new Unet and got some problems with the NetworkServer.spawn method and collisions.
[Command]
public void CmdSpawnProjectile(Vector2 position, Vector2 direction) {
GameObject proj = (GameObject) Instantiate(projectile, position, Quaternion.identity);
Physics2D.IgnoreCollision(proj.GetComponent<Collider2D>(), GetComponent<Collider2D>());
proj.GetComponent<Rigidbody2D>().velocity = direction * force;
Destroy(proj, 2f);
NetworkServer.Spawn(proj);
}
The ignore collision works fine on the host as expected. But that won't apply to the other clients which ofc is logical. The question is how to apply the Physics2D.IgnoreCollision on the clients as well. Everything works perfectly except the projectiles spawned on the clients collides with the player who spawns them.
(Simply spawning the projectiles outside the collision of the spawner is not a option.)
Answer by blasse · Jun 19, 2015 at 03:56 AM
My solution after some diving into the API was:
------- Projectile class -------
[SyncVar]
public NetworkInstanceId spawnedBy;
// Set collider for all clients.
public override void OnStartClient() {
GameObject obj = ClientScene.FindLocalObject(spawnedBy);
Physics2D.IgnoreCollision(GetComponent<Collider2D>(), obj.GetComponent<Collider2D>());
}
------- Spawner class -------
[Command]
public void CmdSpawnProjectile(Vector2 position, Vector2 direction, NetworkInstanceId netId) {
GameObject proj = (GameObject) MonoBehaviour.Instantiate(projectile, position, Quaternion.identity);
proj.GetComponent<Rigidbody2D>().velocity = direction * force;
proj.GetComponent<Projectile>().spawnedBy = netId;
NetworkServer.Spawn(proj);
}
Answer by seanr · Jun 18, 2015 at 09:44 PM
set it in OnStartClient() on a NetworkBehaviour script on the projectile
Where to set the collider was not really the problem. The tricky part was getting hold of the spawners collider in the projectile class after the spawn call in the spawner.
@blasse that indeed is the tricky part, how did you solve that?
Your answer
![](https://koobas.hobune.stream/wayback/20220613201134im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to avoid spawn delay on client? 0 Answers
Networking bullet spawning issues between Host/Client 0 Answers
Since switching to using offline & online scenes in the NetworkManager, Clients will not sync (UNET) 0 Answers
Doubt about how to sync in multiplayer 1 Answer
Spaw dynamic (unregistered) object on network (UNET) 0 Answers