[英]Unity Mirror, Client is not executing damage Command
I have a script on the Player that gets its target by clicking on them.我在播放器上有一个脚本,可以通过单击它们来获取目标。 I check if the players auto attack cooldown is 0 and if the player is in range.
我检查玩家的自动攻击冷却时间是否为 0 以及玩家是否在射程内。 After that it should run a Command and damage the enemy mob.
之后它应该运行一个命令并伤害敌人的生物。
This only happens as it is intended on the host and not on client.这只发生在主机上而不是客户端上。 And if I remove the enemy != null check in the CmdDamage function the client just disconnects.
如果我在 CmdDamage 函数中删除敌人 != null 检查,客户端就会断开连接。
public class PlayerAttacker : NetworkBehaviour公共类 PlayerAttacker : NetworkBehaviour
public EnemyScript enemy;
public float timer = 0;
public float timerMax;
private void Start()
{
timer = timerMax;
}
private void Update()
{
if (!isLocalPlayer)
return;
if (Input.GetMouseButtonDown(0))
{
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 mousePos2d = new Vector2(mousePos.x, mousePos.y);
RaycastHit2D hit = Physics2D.Raycast(mousePos2d, Vector2.zero);
if(hit.collider != null && hit.collider.GetComponent<EnemyScript>() != null)
{
enemy = hit.collider.GetComponent<EnemyScript>();
enemy.target.SetActive(true);
}
}
if (timer <= 0)
{
timer = 0;
BasicAttack();
}
else if(timer > 0)
{
timer -= Time.deltaTime;
}
}
private void BasicAttack()
{
float dist = Vector3.Distance(enemy.transform.position, transform.position);
if(dist < 2.5f)
{
GetComponent<NetworkAnimator>().SetTrigger(Animator.StringToHash("sword slash")); ///SEND TRIGGERS OVER NETWORK
CmdDamage();
timer = timerMax;
}
}
[Command]
private void CmdDamage()
{
if(enemy != null)
enemy.TakeDamage(5);
}
public class EnemyScript : NetworkBehaviour公共类 EnemyScript : NetworkBehaviour
[SyncVar(hook = "OnHealthChanged")] public float currentHealth; //ADD HP BARS INGAME
public float maxHealth;
[SerializeField] public HealthBar healthBar;
public override void OnStartServer()
{
currentHealth = maxHealth;
}
public void TakeDamage(float amount)
{
if (!isServer)
return;
currentHealth -= amount;
}
Where is your OnHealthChanged -hook implemented?您的 OnHealthChanged -hook 在哪里实施? With SyncVars that are hooked, you have to assign changed variable inside hook method.
使用挂钩的 SyncVars,您必须在挂钩方法中分配更改的变量。 In your case, you have to assign a new value for currentHealth inside OnHealthChanged.
在您的情况下,您必须在 OnHealthChanged 中为 currentHealth 分配一个新值。
Now your currentHealth is updated only on server.现在您的 currentHealth 仅在服务器上更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.