I’m having issues with OnTriggerEnter on the projectile. It isn’t getting called. However, if I remove the [ServerCallBack] then it is called (tested with debug.log) but not doing anything - presumably as the client is detecting the collision and not being able to do anything about it.
using Mirror;
using UnityEngine;
public class UnitProjectile : NetworkBehaviour
{
[SerializeField] private Rigidbody myRigidBody = null;
[SerializeField] private int damageToDeal = 20;
[SerializeField] private float launchForce = 10f;
[SerializeField] private float destroyAfterSeconds = 5f;
private void Start()
{
myRigidBody.velocity = transform.forward * launchForce;
}
public override void OnStartServer()
{
Invoke(nameof(DestroySelf), destroyAfterSeconds);
}
[ServerCallback]
private void OnTriggerEnter(Collider other)
{
if (other.TryGetComponent<NetworkIdentity>(out NetworkIdentity networkIdentity))
{
if (networkIdentity.connectionToServer == connectionToServer) { return; }
}
if (other.TryGetComponent<Health>(out Health health))
{
health.DealDamage(damageToDeal);
}
DestroySelf();
}
[Server]
private void DestroySelf()
{
NetworkServer.Destroy(gameObject);
}
}
using Mirror;
using System;
using UnityEngine;
public class Health : NetworkBehaviour
{
[SerializeField] private int maxHealth = 100;
[SyncVar] private int currentHealth;
public event Action ServerOnDie;
#region Server
public override void OnStartServer()
{
currentHealth = maxHealth;
}
[Server]
public void DealDamage(int damageAmount)
{
if (currentHealth == 0) { return; }
currentHealth = Mathf.Max(currentHealth - damageAmount, 0);
if (currentHealth != 0) { return; }
ServerOnDie?.Invoke();
Debug.Log("Unit died");
}
#endregion
#region Client
#endregion
}