Question by
Joyble · Sep 25, 2016 at 03:51 PM ·
networkingmeleehealth
Help with multiplayer melee
Friend and I have been working on a game for a bit now. We've implemented melee combat and it's working just great in singleplayer. But after we've implemented multiplayer, we noticed that the weapon does no damage at all. Since we're almost complete beginners to C# and Unity in general, I was wondering if someone could tell me how to edit these two scripts so that they work in multiplayer.
Main weapon code:
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class MaceAttack : NetworkBehaviour {
[SyncVar]public float damage = 50.0F;
public float attackDuration = 0.5F;
public bool attacking = false;
[HideInInspector]
void Start ()
{
}
void Update()
{
if (Input.GetButtonDown ("Light Attack")) {
attacking = true;
} else {
attacking = false;
}
}
void OnTriggerEnter (Collider col)
{
if(col.tag == "Enemy")
{
if(attacking)
{
col.SendMessage("receiveDamage", damage ,SendMessageOptions.DontRequireReceiver);
}
}
}
void EnableDamage()
{
if (attacking == true) return;
attacking = true;
StartCoroutine("DisableDamage");
}
IEnumerator DisableDamage()
{
yield return new WaitForSeconds(attackDuration);
attacking = false;
}
}
Health code:
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class EnemyHealth : NetworkBehaviour {
public float maxHP = 100.0F;
public float currentHP;
public GameObject Enemy;
public int playerID;
// Use this for initialization
void Start () {
currentHP = maxHP;
}
// Update is called once per frame
void Update () {
checkStatus();
}
public void checkStatus(){
if(currentHP > maxHP)
currentHP = maxHP;
if(currentHP < 0)
currentHP = 0;
if(currentHP == 0)
death();
}
public void receiveDamage(float damage){
if (isServer)
{
return;
}
currentHP -= damage;
Debug.Log("Damage Applied");
}
public void death(){
Destroy(this.gameObject);
}
}
Comment