首页 > 解决方案 > Unity3D多人游戏中相机的奇怪行为

问题描述

我不熟悉统一,并且正在为我的相机的奇怪行为而苦苦挣扎。我正在使用 Photon Plugin 编写在线多人游戏。

奇怪的行为:

(但运动控制对每个玩家都有效)

我得到了一个角色的预制件,它在检查器中安装了一个摄像头。

这是我的初始化代码:

public class NetworkPlayer  : Photon.Pun.MonoBehaviourPun, Photon.Pun.IPunObservable
{
    public Animator anim;
    private Vector3 correctPlayerPos;
    private Quaternion correctPlayerRot;
    public GameObject myCam;
    // Start is called before the first frame update
    void Start()
    {
        if(photonView.IsMine)
        {
            Debug.Log("PhotonView.IsMine == true");
            //Kamera und Steuerung aktivieren
            myCam = GameObject.Find("Camera");
            myCam.SetActive(true);
            GetComponent<PlayerMovement>().enabled = true;
            Debug.Log("Steuerung und Cam aktiviert...");
        }
    }

    // Update is called once per frame
    void Update()
    {
        if(!photonView.IsMine)
        {
            transform.position = Vector3.Lerp(transform.position, this.correctPlayerPos, Time.deltaTime*5);
            transform.rotation = Quaternion.Lerp(transform.rotation, this.correctPlayerRot, Time.deltaTime * 5);
        }
    }

    //Exchange Position data
    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info){
        if(stream.IsWriting)
        {
            //Send data to others
            stream.SendNext(transform.position);
            stream.SendNext(transform.rotation);
            stream.SendNext(anim.GetBool("Run"));
        } 
        else 
        {
            //Receive data from others
            this.correctPlayerPos = (Vector3) stream.ReceiveNext();
            this.correctPlayerRot = (Quaternion) stream.ReceiveNext();
            anim.SetBool("Run", (bool) stream.ReceiveNext());
        }
    }
}

我还尝试通过检查器连接相机,而不是像上面的代码示例中那样搜索它。希望任何人都可以帮助我:(

感谢您的时间!

标签: unity3dcameramultiplayerphoton

解决方案


我的猜测是你SetAtive(true)在播放器上做了,但从你的照片来看,相机默认情况下似乎是激活的!因此,每个新相机都添加到 Hierachy 中先前相机的下方,这使其成为顶部渲染的相机。

您可以简单地禁用Camera播放器预制件中的 。

无论如何,您还应该主动为脚本中的其他玩家禁用相机,这样您就不会忘记/不必依赖将您的预制件设置为特定状态

public class NetworkPlayer  : Photon.Pun.MonoBehaviourPun, Photon.Pun.IPunObservable
{
    public Animator anim;
    // you are referncing this for the prefab via the inspector
    // I would also use the proper type as a safety
    // this way you can't by accident reference something else here but a Camera
    public Camera myCam;

    // you should also this already via the inspector
    public PlayerMovement playerMovement;

    private Vector3 correctPlayerPos;
    private Quaternion correctPlayerRot;

    // Use Awake to gather components and references as fallback
    // if they where not referenced via the Inspector
    private void Awake()
    {
        FetchComponents();
    } 

    // Fetches components only if they where not referenced via the Inspector
    // this saves unnecessary GetComponent calls which are quite expensive
    //
    // + personal pro tip: By using the context menu you can now also in the Inspector simply click on FetchComponents
    // This way you 
    //   a) don't have to search and drag them in manually
    //   b) can already see if it would also work later on runtime 
    [ContextMenu(nameof(FetchComponents))]
    private void FetchComponents()
    {
        if(!playerMovement) playerMovement = GetComponent<PlayerMovement>();
        if(!anim) anim = GetComponent<Animator>();
        // Use GetComponnetInChildren to retrieve the component on yourself
        // or recursive any of your children!
        // Pass true in order to also get disabled or inactive ones
        if(!myCam) myCam = GetComponentInChildren<Camera>(true);
    }       

    // Now do your thing not only for the player but also actively disable the stuff
    // on all other players
    private void Start()
    {
        var isMine = photonView.IsMine;
        Debug.Log($"PhotonView.IsMine == {isMine}");
        // enable the cam for you, disable them for others!
        myCam.gameObject.SetActive(isMine);
        // enable controls for you, disable them for others
        playerMovement.enabled = isMine;

        Debug.Log($"Steuerung und Cam {isMine ? "" : "de"}aktiviert...");
    }

    ...
}

推荐阅读