Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

photon player movement problum

Discussion in 'Scripting' started by mmomaker, Feb 2, 2015.

  1. mmomaker

    mmomaker

    Joined:
    Mar 12, 2013
    Posts:
    15
    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
     
  2. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    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.
     
  3. TidyDev

    TidyDev

    Joined:
    Dec 16, 2012
    Posts:
    45
    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:

    Code (CSharp):
    1.  
    2. using ExitGames.Client.Photon;
    3.  
    4. public class PlayerMovement : Photon.MonoBehaviour
    5. {
    6.     public float speed = 1f;
    7.  
    8.     void Start()
    9.     {
    10.  
    11.     }
    12.  
    13.     void Update()
    14.     {
    15.         MovePlayer();
    16.     }
    17.  
    18.     void MovePlayer()
    19.     {
    20.         if(photnoView.isMine)
    21.         {
    22.             if(Input.GetKey(KeyCode.D))
    23.             transform.position += new Vector3(speed *Time.deltaTime,0.0f,0.0f);
    24.  
    25.             if(Input.GetKey(KeyCode.A))
    26.             transform.position -= new Vector3(speed *Time.deltaTime,0.0f,0.0f);
    27.  
    28.             if(Input.GetKey(KeyCode.W))
    29.             transform.position += new Vector3(0.0f, speed *Time.deltaTime,0.0f);
    30.  
    31.             if(Input.GetKey(KeyCode.S))
    32.             transform.position -= new Vector3(0.0f, speed *Time.deltaTime,0.0f);
    33.         }
    34.     }
    35. }
    36.  
    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 :D
     
    Polymorphik likes this.
  4. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    Thats the one hahaha
     
  5. mmomaker

    mmomaker

    Joined:
    Mar 12, 2013
    Posts:
    15
    wait wut photon never showd eney thing about OnPhotonSerializeView in it demos how do we set that up
     
  6. TidyDev

    TidyDev

    Joined:
    Dec 16, 2012
    Posts:
    45
    for example:

    Code (CSharp):
    1. // Photon Serialize Events
    2. void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info) {
    3.     // If we are the player
    4.     if (stream.isWriting) {
    5.         // Set TEMP vars
    6.         TEMP_pos = transform.position;                             //position
    7.         TEMP_rot = transform.rotation;                             //rotation
    8.  
    9.         // Stream values accross the network
    10.         stream.Serialize(ref TEMP_pos);
    11.         stream.Serialize(ref TEMP_rot);
    12.        
    13.         // Example on syncing animation ( a = Animator component )
    14.         stream.SendNext(a.GetBool("aiming"));
    15.        
    16.     } else { // If we are not the player
    17.  
    18.         // Recieve streams from others
    19.         stream.Serialize(ref TEMP_pos);
    20.         stream.Serialize(ref TEMP_rot);
    21.        
    22.         // Recieve all animations from the network #note these must be in the same order that the
    23.         // animation streams are sent
    24.         a.SetBool("aiming", (bool)stream.ReceiveNext());
    25.        
    26.         // Apply remote values to remote players
    27.         h = TEMP_h;
    28.         v = TEMP_v;
    29.     }
    30. }
    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