Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
154 views
in Technique[技术] by (71.8m points)

c# - Pun 2 Synchronizing

I created a multiplayer game with pun2 (Photon Unity Networking) but when I move and look actually changes of my character will apply to other characters, also when my friends move their changes will apply to my character here is movement scripts

and here is my scripts for creating room and joining

question from:https://stackoverflow.com/questions/65880806/pun-2-synchronizing

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For the future: Please add code as TEXT not as images to your questions!


In your Update method you listen to global keyboard events. These will be executed on all instances of that script.

You should check if the component actually belongs to your own player and otherwise ignore it or even better disable the entire component!

See Photon User Input Management

// Note that inheriting from MonoBehaviourPun instead of MonoBehaviour is mandatory!
public class YourClass : MonoBehaviourPun
{
    ...

    private void Update()
    {
        // As also described in the link the IsConnected is checked in order to allow 
        // offline debugging of your behavior without being in a network session
        if(PhotonNetwork.IsConnected && !photonView.IsMine)
        {
            // optional but why keep an Update method running of a script that 
            // shall anyway only be handled by the local player?
            //enabled = false;
            // Or you could even remove the entire component
            //Destroy(this);
    
            // Do nothing else
            return;
        }
    
        ...
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...