简体   繁体   English

PUN2 | 该过程被调用两次

[英]PUN2 | The process is called twice

I am currently using PUN2 to create a multiplayer shooting game.我目前正在使用 PUN2 来创建多人射击游戏。 Participation in a room and synchronization of movement and bullets have been implemented.已实现参与房间和同步移动和子弹。

However, the processing when hit is performed twice.但是,命中时的处理进行了两次。 Perhaps this is because the exact same object that exists on the two devices is processing each shot.也许这是因为两个设备上存在的完全相同的对象正在处理每个镜头。 (maybe) (也许)

How do I avoid duplicate hit handling?如何避免重复命中处理?

Code:代码:

flow : The shooter assigns its own identification number to the shell and fires it → Anyone who hits the shell will take damage.流程:射手将自己的识别号分配给炮弹并开火→任何击中炮弹的人都会受到伤害。

Player.cs播放器.cs

private void OnCollisionEnter(Collision collider)
{
        BulletController bulletController = collider.gameObject.GetComponent<BulletController>();

        if (photonView.Owner.ActorNumber != bulletController.shooterUserId)
        {
            photonView.RPC(nameof(Damaged), RpcTarget.All, bulletController.Damage, bulletController.shooterUserId);
            bulletController.Remove();
        }
}

[PunRPC]
void Damaged(int damage, int userId)
{
    var hashtable = new Hashtable();
    hashtable["HP"] = HP;
    PhotonNetwork.LocalPlayer.SetCustomProperties(hashtable);

    if (photonView.IsMine)
    {
        HP -= damage;
        if (HP <= 0)
        {
            this.gameObject.SetActive(false);
            HP = 0;

            if (userId != photonView.OwnerActorNr)
                photonView.RPC("KilledPlayer", RpcTarget.Others);

        }
    }
}

[PunRPC]
void KilledPlayer()
{
    GameSparksLeaderboard.SaveXP(100);
}

I just forgot to attach photonView.IsMine .我只是忘了附上photonView.IsMine It's important, but it's easy to forget ...这很重要,但很容易忘记......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM