Quantcast
Channel: Questions in topic: "ignorecollision"
Viewing all articles
Browse latest Browse all 148

UNET: How to ignore collisions

$
0
0
Ok so I am instantiating a prefab, which works fine on both the host and all clients. Now my issue is as soon as I add a BoxCollider2D the instantiated prefab will right away collide with the player. In order to solve that I use `Physics2D.IgnoreCollision` - however this only works on the host, not on any other clients since it's a Command. My code is as followed: using UnityEngine; using System.Collections; using UnityEngine.Networking; public class player_projectile : NetworkBehaviour { public GameObject projectile; public float ProjectileSpeed = 5f; GameObject square; [Client] void Update() { if (Input.GetMouseButtonDown(0)) { GetComponent().Play (); Vector2 direction = (Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z +10)) - transform.position).normalized; CmdShootProjectile(direction, ProjectileSpeed); } } [Command] void CmdShootProjectile(Vector2 direction, float speed) { square = Instantiate(projectile, transform.position, transform.rotation) as GameObject; NetworkServer.Spawn(square); Physics2D.IgnoreCollision(gameObject.GetComponent(), square.GetComponent()); square.GetComponent().velocity = direction * speed; Destroy(square, 2); } } As you can see I have `Physics2D.IgnoreCollision` under `CmdShootProjectile` but this only makes it work for the host. How can I also make it work for every other client connected so they don't collide with their own projectile?

Viewing all articles
Browse latest Browse all 148


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>