在Unity中使用Photon与玩家不同步

发布于 2025-02-14 02:22:25 字数 1872 浏览 25 评论 0 原文

我最近开始使用Photon创建多人游戏游戏,我的教程在此处链接:。正如您可以从此问题的标题中读取的那样,玩家的动作不是同步。

在这里,如果我的播放器代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using Photon.Pun;

public class playerControls : MonoBehaviour
{
    [SerializeField] private Rigidbody2D body;
    [SerializeField] public Transform bodyTransform;
    public float speed;
    private float horizontalInput;
    public PhotonView view;

    // Update is called once per frame
    void Update()
    {
        if(view.IsMine){
            if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.D))
            {
                bodyTransform.localScale = new Vector3(1.3f,1.3f,1.3f);
            }
            else if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.A))
            {
                bodyTransform.localScale = new Vector3(-1.3f,1.3f,1.3f);
            }
            horizontalInput = Input.GetAxis("Horizontal");
            body.velocity = new Vector2(horizontalInput * (float)speed, body.velocity.y);
            if(Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.W)){
                body.velocity=new Vector2(body.velocity.x,6);
            }
        }
    }
}

我还使用了光子变换视图,但是它仍然不起作用。

其他一切正常,您可以单独移动每个玩家。您可以加入房间。玩家产卵。但是,唯一的问题是播放器同步不起作用。任何反馈都将得到理解-Jake

Sidenote,它在这里可以播放:

I have recently started using Photon to create multiplayer games, my tutorial is linked here: https://www.youtube.com/watch?v=93SkbMpWCGo . As you can read from the title of this problem, the players movements are not syncing.

here if the code for my player:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using Photon.Pun;

public class playerControls : MonoBehaviour
{
    [SerializeField] private Rigidbody2D body;
    [SerializeField] public Transform bodyTransform;
    public float speed;
    private float horizontalInput;
    public PhotonView view;

    // Update is called once per frame
    void Update()
    {
        if(view.IsMine){
            if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.D))
            {
                bodyTransform.localScale = new Vector3(1.3f,1.3f,1.3f);
            }
            else if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.A))
            {
                bodyTransform.localScale = new Vector3(-1.3f,1.3f,1.3f);
            }
            horizontalInput = Input.GetAxis("Horizontal");
            body.velocity = new Vector2(horizontalInput * (float)speed, body.velocity.y);
            if(Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.W)){
                body.velocity=new Vector2(body.velocity.x,6);
            }
        }
    }
}

I have also used the Photon Transform View, however it still does not seem to work.

Everything else works fine, you can move each player individually. You can join rooms. And the players spawn. The only problem however is just that the player sync does not work. Any feedback would be appreceated -Jake

Sidenote it is playable here: https://chartreusefinancialdifferences.jake-is-theis.repl.co/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三五鸿雁 2025-02-21 02:22:25

PhotonTransFormView只是可以通过网络同步的组件。您还需要PhotonView将实际同步和拖放到观察到的组件列表中。因此,基本上,PhotonView是网络化的最重要组件,PhotonTransFormView只是来自转换的序列化/进行序列化值。如果添加任何实现ipunobservable的类

PhotonTransformView is just a component that can be synced over the network. You also need PhotonView that does actual sync and drag-and-drop your PhotonTransformView to the Observed Components list. So basically the PhotonView is most important component for networking, PhotonTransformView is just a serializing/deserializing values from transform. If you add any class implementing IPunObservable you need to drag them to the Observed Components list to work

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文