photon player movement problum

Hi guys my name is cuddle bunny and me and a few friends are working on a 2d top down social mmo like club pengrin but we have hit a wall and none of us can figure out how to fix it
Here is are movement scrip
using UnityEngine;
using System.Collections;

public class PlayerMovement :MonoBehaviour
{
public float speed = 1f;

void Start()
{

}

void Update()
{
if(Input.GetKey(KeyCode.D))
transform.position += new Vector3(speed *Time.deltaTime,0.0f,0.0f);

if(Input.GetKey(KeyCode.A))
transform.position -= new Vector3(speed *Time.deltaTime,0.0f,0.0f);

if(Input.GetKey(KeyCode.W))
transform.position += new Vector3(0.0f, speed *Time.deltaTime,0.0f);

if(Input.GetKey(KeyCode.S))
transform.position -= new Vector3(0.0f, speed *Time.deltaTime,0.0f);
}
}
the problem we are having is wean some one move their player it move every one else on the person screen but on every one else screen that person dint mover every one and is moved to the rite place that they wanted to go the only way we have found to fix this is to have the other player move to up date the others screen but then that person get the bug how can we fix this were it doesn’t do this any more

I bought Photon but I have not yet used it, however I think they said its recommended to use Photon.MonoBehaviour and also if you are going to be using movement scripts you can check the instance by using Photon.IsMe (forgot) that should solve the problem.

the issue is that you are not telling photon who owns the instantiated object, you need to use photonView.isMine your code should look more like this:

using ExitGames.Client.Photon;

public class PlayerMovement : Photon.MonoBehaviour
{
    public float speed = 1f;

    void Start()
    {

    }

    void Update()
    {
        MovePlayer();
    }

    void MovePlayer()
    {
        if(photnoView.isMine)
        {
            if(Input.GetKey(KeyCode.D))
            transform.position += new Vector3(speed *Time.deltaTime,0.0f,0.0f);

            if(Input.GetKey(KeyCode.A))
            transform.position -= new Vector3(speed *Time.deltaTime,0.0f,0.0f);

            if(Input.GetKey(KeyCode.W))
            transform.position += new Vector3(0.0f, speed *Time.deltaTime,0.0f);

            if(Input.GetKey(KeyCode.S))
            transform.position -= new Vector3(0.0f, speed *Time.deltaTime,0.0f);
        }
    }
}

But this is just the start, you are going to have to tell photon that if it isnt your character get the position and rotation of the network game object, to do this we use OnPhotonSerializeView to send and receive streams, this is how we can sync everything between all players such as movement and animations.

I strongly suggest watching and following this tutorial, it will help you to understand Photon networking, https://www.youtube.com/playlist?list=PLbghT7MmckI7BDIGqNl_TgizCpJiXy0n9

Good luck :smile:

1 Like

Thats the one hahaha

wait wut photon never showd eney thing about OnPhotonSerializeView in it demos how do we set that up

for example:

// Photon Serialize Events
void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info) {
    // If we are the player
    if (stream.isWriting) {
        // Set TEMP vars
        TEMP_pos = transform.position;                             //position
        TEMP_rot = transform.rotation;                             //rotation

        // Stream values accross the network
        stream.Serialize(ref TEMP_pos);
        stream.Serialize(ref TEMP_rot);
       
        // Example on syncing animation ( a = Animator component )
        stream.SendNext(a.GetBool("aiming"));
       
    } else { // If we are not the player

        // Recieve streams from others
        stream.Serialize(ref TEMP_pos);
        stream.Serialize(ref TEMP_rot);
       
        // Recieve all animations from the network #note these must be in the same order that the
        // animation streams are sent
        a.SetBool("aiming", (bool)stream.ReceiveNext());
       
        // Apply remote values to remote players
        h = TEMP_h;
        v = TEMP_v;
    }
}

try to receive them in the same order they was sent, refer here for more detail > http://doc-api.exitgames.com/en/pun/current/pun/doc/annotated.html