Hello guys!
I made a simple 3d game using the unity UNET. The problem i am having is that when i join the host as client the client can't deal damage. Help me fix this, please.
----------
void OnTriggerEnter(Collider col)
{
if (isAttacking && alreadyAttacked == false && col.gameObject.tag=="Enemy")
{
alreadyAttacked = true;
if (SceneManager.GetActiveScene().name == "Arena")
{
col.gameObject.GetComponent().TakeDamage(damage);
}
}
}
This code gets called when the sword's box collider enters the enemy's collider and sends the damage to the enemy's script.
public const float maxHealth = 100;
[SyncVar(hook ="OnChangeHealth")] public float currentHealth = maxHealth;
public RectTransform healthbar;
public void TakeDamage(float amount)
{
if(!isServer)
{
return;
}
currentHealth -= amount;
if(currentHealth<=0)
{
Destroy(this.gameObject);
}
}
void OnChangeHealth(float currentHealth)
{
healthbar.sizeDelta = new Vector2(currentHealth * 2, healthbar.sizeDelta.y);
}
↧