H.Atef H.Atef - 1 month ago 18
C# Question

Send player position to another player to move him in multi player game using Unity Photon

I follow the steps of PUN basic tutorial.
For now I reach to a part of what I want Send my position in the current time to another player with me in the room to move him. I can print my position each time I update it , what I need is to know how I can send position to another player to move him.
Let's say I have a desktop player and when I move him this translation moving the player on mobile.
And how I stop instantiate object on mobile, I just want to deal with the instantiated object on desktop.
I am using unity and Photon Network SDK.

Here is the code I used

using UnityEngine;
using System.Collections;

public class NetworkCharacter : Photon.PunBehaviour {

private Vector3 correctPlayerPos;

void Update()
{
if (!photonView.isMine)
transform.position = Vector3.Lerp(transform.position, this.correctPlayerPos, Time.deltaTime * 5);

photonView.RPC ("PrintPosition", PhotonTargets.All, transform.position);
}

void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.isWriting)
{
// We own this player: send the others our data
stream.SendNext(transform.position);
}
else
{
// Network player, receive data
this.correctPlayerPos = (Vector3)stream.ReceiveNext();
}
}

[PunRPC]
void PrintPosition(Vector3 pos)
{
Debug.Log (pos);
//I need to send position coordinates to the other device
}
}


The other class of establish multiplayer environment:

using UnityEngine;
using System.Collections;

public class NetworkManager : Photon.PunBehaviour {

// Use this for initialization
void Start () {
PhotonNetwork.ConnectUsingSettings ("0.1");
//PhotonNetwork.logLevel = PhotonLogLevel.Full;
}

void Update () {

}

void OnGUI()
{
GUILayout.Label(PhotonNetwork.connectionStateDetailed.ToString());
}

public override void OnJoinedLobby ()
{
Debug.Log("join lobby!");
PhotonNetwork.JoinRandomRoom ();
}

void OnPhotonRandomJoinFailed()
{
Debug.Log("Can't join random room!");
PhotonNetwork.CreateRoom(null);
}

void OnJoinedRoom()
{
Debug.Log("join random room!");
GameObject monster = PhotonNetwork.Instantiate("monsterprefab", Vector3.zero, Quaternion.identity, 0);
}
}

Answer

PUN has some new components since their new update. I'd recommend you to use those components because it is really user friendly towards new users.

Some components you could use:

  • PhotonTransformView
  • PhotonRigidbodyView
  • PhotonAnimatorView

These three components will help you sync your position, physics and animations on the GameObject it is attached to.

If you do not want to use these components i suggest you search up: interpolation and extrapolation for Unity PUN.

Some good starting tutorials:

  • SkyArena PUN Tutorial This is a really good video tutorial[in parts] on PUN so you should definitely check that out.
  • Exit Games Marco Polo This tutorial is created by someone from PUN also really good to read even if you don't need it.

Hope this will help you.

-Menno

Comments