Hi,
Multiplayer course
This is my unit.cs on method ServerHandleDie I put some animation on line 51 and made a coroutine so the unit would play the animation before it gets destroyed the animation plays and the unit gets destroyed after but now my problem is while the animation is playing the opposing side (Build side) is still able to target my unit even the health is 0
public class Unit : NetworkBehaviour
{
[SerializeField] UnitMovement unitMovement;
[SerializeField] private Targeter targeter = null;
[SerializeField] private UnityEvent onSelected = null;
[SerializeField] private UnityEvent onDeSelected = null;
[SerializeField] private Health health = null;
[SerializeField] private Animator animator = null;
public static event Action<Unit> ServerOnUnitSpawned;
public static event Action<Unit> ServerOnUnitDeSpawned;
public static event Action<Unit> AuthorityOnUnitSpawned;
public static event Action<Unit> AuthorityOnUnitDeSpawned;
public UnitMovement GetUnitMovement()
{
return unitMovement;
}
public Targeter GetTargeter()
{
return targeter;
}
#region Server
public override void OnStartServer()
{
ServerOnUnitSpawned?.Invoke(this);
health.ServerOnDie += ServerHandleDie;
}
public override void OnStopServer()
{
health.ServerOnDie -= ServerHandleDie;
ServerOnUnitDeSpawned?.Invoke(this);
}
[Server]
private void ServerHandleDie() //Handles Destroy For Unit
{
animator.SetBool("isDead", true);
StartCoroutine(OnsDieServerHandle());
}
IEnumerator OnsDieServerHandle()
{
yield return new WaitForSeconds(2);
NetworkServer.Destroy(gameObject);
}
#endregion
#region Client
public override void OnStartAuthority()
{
AuthorityOnUnitSpawned?.Invoke(this);
}
public override void OnStopClient()
{
if (!isOwned) { return; }
AuthorityOnUnitDeSpawned?.Invoke(this);
}
[Client]
public void Select()
{
if(!isOwned) return;
onSelected?.Invoke();
}
[Client]
public void DeSelect()
{
if (!isOwned) return;
onDeSelected?.Invoke();
}
#endregion
}
I tried to clear my target on my targeter Script on but it still attacks my unit
Targeter.cs
public class Targeter : NetworkBehaviour
{
private Targetable target;
[SerializeField]private Health health;
public Targetable GetTarget()
{
return target;
}
public override void OnStartServer()
{
GameOverHandler.ServerOnGameOver += ServerHandleGameOver;
health.ServerOnDie += ServerStopAttacking;
}
public override void OnStopServer()
{
GameOverHandler.ServerOnGameOver -= ServerHandleGameOver;
health.ServerOnDie -= ServerStopAttacking;
}
[Command]
public void CmdSetTarget(GameObject targetGameobject)
{
if (!targetGameobject.TryGetComponent<Targetable>(out Targetable newTarget)) return;
target = newTarget;
}
[Server]
public void ClearTarget()
{
target = null;
}
[Server]
private void ServerHandleGameOver()
{
ClearTarget();
}
[Server]
private void ServerStopAttacking()
{
ClearTarget();
Debug.Log("cleared");
}
}
Anyone have any idea how to stop the attacking once my unit’s health turn to 0?
Thanks in advance