Sync movements Photon
Hi, I'm trying to make my first 2D multiplayer game with Photon(Photon 2), but I have a problem: I can't synchronize the movements. I have alredy added Photon View, Photon RigidBody 2D and Photon Transform View to the player's prefab. The playerobject isn't part of the scene yet. I instantiate it. For instantiate I'use this script(also check some buttons):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class photonButtons : MonoBehaviour
{
public InputField createInput, joinInput, nameInput;
public GameObject mainPlayer;
public string Name;
public void Awake() {
DontDestroyOnLoad(this.transform);
}
void OnEnable()
{
SceneManager.sceneLoaded += OnLevelFinishedLoading;
}
void OnDisable()
{
SceneManager.sceneLoaded -= OnLevelFinishedLoading;
}
void OnLevelFinishedLoading(Scene scene, LoadSceneMode mode)
{
if(scene.name == "Game"){
spawnPlayer();
}
}
public void onClickConnect(){
Name = nameInput.text;
RoomOptions roomOptions = new RoomOptions();
roomOptions.MaxPlayers = 20;
PhotonNetwork.JoinOrCreateRoom(joinInput.text, roomOptions, TypedLobby.Default);
}
public void onClickCreate(){
Name = nameInput.text;
if(createInput.text.Length > 1)
PhotonNetwork.CreateRoom(createInput.text, new RoomOptions(){MaxPlayers = 20}, null);
}
private void OnJoinedRoom(){
PhotonNetwork.LoadLevel("Game");
}
private void OnLeaveRoom(){
}
private void spawnPlayer(){
PhotonNetwork.Instantiate(mainPlayer.name, mainPlayer.transform.position, mainPlayer.transform.rotation, 0);
}
}
For the movements I use this script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class plControlls : Photon.MonoBehaviour
{
public bool devTesting = false;
public PhotonView pothonView;
public string Name;
public float speed;
GameObject SceneCamera;
public GameObject PlayerCamera;
Rigidbody2D rigidbody2d;
Vector2 selfPos;
public GameObject photonScript;
public GameObject nText;
void Awake()
{
if(!devTesting && photonView.isMine){
SceneCamera = GameObject.Find("MainCamera");
SceneCamera.SetActive(false);
PlayerCamera.SetActive(true);
}
enabled = photonView.isMine;
rigidbody2d = GetComponent<Rigidbody2D>();
photonScript = GameObject.Find("photonScript");
nText = GameObject.Find("NameText");
string Name = photonScript.GetComponent<photonButtons>().Name;
nText.GetComponent<TextMesh>().text = Name;
}
void Update()
{
if(!devTesting && photonView.isMine){
checkInput();
}
else{
smoothNetMovement();
}
}
private void checkInput(){
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
Vector2 position = rigidbody2d.position;
position.x = position.x + speed * horizontal * Time.deltaTime;
position.y = position.y + speed * vertical * Time.deltaTime;
rigidbody2d.MovePosition(position);
}
private void smoothNetMovement(){
transform.position = Vector2.Lerp(transform.position, selfPos, Time.deltaTime * 8);
}
}
Thanks for Help!
Hey there and welcome to the forum,
can you add some more information on your project?
Is your playerobject already part of the scene or do you instantiate it? if the latter then what do you use for instantiation?
Do you use Photon 1 or 2?
As a sidenote: you should probably remove the line public PhotonView pothonView;
since this should already be given by deriving from Photon.$$anonymous$$onobehaviour
. (At least in Photon 1 as can be seen here)
Hi, thanks for the comment. I added the information inthe question. Thanks for help!
Cool, so far it all looks fine to me. When is the smoothNet$$anonymous$$ovement function for? Or what is the intention of it? since i cannot see the you setting selfPos
anywhere this would always overwrite the position that you only get every 0.1 seconds and try to lerp to Vector2.Zero i guess.