- Home /
Unet collision and layer mask issues
Thanks for checking out my post! I am making a multiplayer game in which the players shoot fireballs at one another. The issue is if the player can kill themselves with their own projectiles if they look down and fire. I have been trying to fix this in two dirrent ways. I have created a layer mask system in which the local player gets assigned a layer called "isLocalPlayer", and the non local players get assigned to a layer called "notLocalPlayer". I then added an additional layer called "Spell", and attached that to my projectile and made sure in the physics tab to uncheck the box between "Spell" and "isLcoalPlayer". However I can still shoot myself when I join the game as a client. When hosting the game, the player cannot shoot themselves. I am not sure why this is but maybe you can take a look at my code to see. Thanks for your time!
//Assigns the local player the correct tag and layer, and is attached to the player
void Update () {
if (!isLocalPlayer) {
cam.enabled = false;
listener.enabled = false;
gameObject.layer = notLocalPlayerLayer;
return;
}
gameObject.layer = isLocalPlayerLayer;
gameObject.tag = "LocalPlayer";
MovePlayer ();
}
// This is the code attached to the spell/projectile
void OnCollisionEnter (Collision col) {
if (col.collider.gameObject.tag == "LocalPlayer") {
return;
}
var hit = col.gameObject;
var health = hit.GetComponent<Health> ();
if (health != null) {
health.TakeDamage (50);
}
Destroy (gameObject);
}
// This is the code that apply damage
public void TakeDamage (int amount) {
if (!isServer) {
return;
}
currentHealth -= amount;
if (currentHealth <= 0) {
spawnDelay ();
}
}