通过Photon同步游戏对象子项的逻辑翻转

问题描述 投票:1回答:1

[在我的项目中,我正在通过光子网络创建类似“疯狂战斗”的游戏,但是是多人游戏。我目前遇到一个问题,就是错误的客户“手+枪”被翻转了。

[当客户1翻转其角色时,他的手在本地被翻转,而客户2的手则在本地被翻转。

图片:https://imgur.com/a/NkP3KEF

我相信问题如下:在角色翻转逻辑中,我要求使用PunRPC来翻转错误的客户手。

主角翻转功能:

    void FixedUpdate()
    {
    if (!disableMove)
    {

        //if (!devTesting)
        //{

        if (photonView.isMine)
        {
            isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
            moveInput = Input.GetAxis("Horizontal");
            rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);

            if (facingRight == false && moveInput > 0)
            {
                photonView.RPC("Flip", PhotonTargets.AllBuffered);
                photonView.RPC("Fliphand", PhotonTargets.AllBuffered); //FLIPS HAND+GUN
                //Flip();
            }
            else if (facingRight == true && moveInput < 0)
            {
                photonView.RPC("Flip", PhotonTargets.AllBuffered);
                photonView.RPC("Fliphand", PhotonTargets.AllBuffered); //FLIPS HAND+GUN
                //Flip();
            }

        }
        }

上面的函数需要这两个PunRPC函数。

字符翻转:

    [PunRPC]
void Flip()
{
        facingRight = !facingRight;     //toggle flipping
        GetComponent<SpriteRenderer>().flipX = !facingRight;
}

手翻转(上述游戏对象的子代):

    [PunRPC]
void Fliphand()
{
        child = GameObject.Find("Hand");
        sight = GameObject.Find("Granny_warr_lasersight");
        deagle = GameObject.Find("deagle_animation");

        facingRight2 = !facingRight2;
        child.GetComponent<SpriteRenderer>().flipY = !facingRight2;
        sight.GetComponent<SpriteRenderer>().flipY = !facingRight2;
        deagle.GetComponent<SpriteRenderer>().flipY = !facingRight2;
}

[我认为问题在于,因为两个客户端都加入了会议室,并且两个客户端都有子对象,称为“手”等。通过从客户端1调用此PunRPC,它会在客户端2的屏幕上翻转“手”。

有人可以帮我解决这个问题吗?如果需要,我会尝试使其更加清晰,并根据要求提供更多图片。

注意:字符翻转在两个客户端上都可以正常工作。

unity3d photon
1个回答
2
投票

由于Tomer Shahar提供的链接,我解决了它!

    [PunRPC]
void Fliphand()
{
    //if (photonView.isMine)
    //{
    //child = GameObject.Find("Hand");
    //sight = GameObject.Find("Granny_warr_lasersight");
    //deagle = GameObject.Find("deagle_animation");

    child = player.transform.Find("Hand").gameObject;
    sight = child.transform.Find("Granny_warr_lasersight").gameObject;
    deagle = child.transform.Find("deagle_animation").gameObject;

    facingRight2 = !facingRight2;
        child.GetComponent<SpriteRenderer>().flipY = !facingRight2;
        sight.GetComponent<SpriteRenderer>().flipY = !facingRight2;
        deagle.GetComponent<SpriteRenderer>().flipY = !facingRight2;
    //}
}

///已禁用的代码是对我的应用没有用的旧代码。新的作品就像一种魅力!

© www.soinside.com 2019 - 2024. All rights reserved.