0

Hi I'm trying photon multiplayer movement for the first time, there seems to be issue when I move my player the other connected person sees the player which I'm moving & not theirs. Below is the code.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;

public class Player_Movement : MonoBehaviour
{
    [SerializeField] float speed;

    [SerializeField] float xDirection;
    [SerializeField] float zDirection;

    [SerializeField] Vector3 moveDirection;

    private PhotonView view;

    private void Start()
    {
        view = GetComponent<PhotonView>();
    }
    void Update()
    {
        if (view.IsMine)
        {
            xDirection = Input.GetAxis("Horizontal");
            zDirection = Input.GetAxis("Vertical");

            moveDirection = new Vector3(xDirection, 0.0f, zDirection);

            transform.position += moveDirection * speed * Time.deltaTime;
        }
    }
}

I have made camera component as child of the player.

IEnumerator Start()
{
    yield return new WaitUntil(() => PhotonNetwork.IsConnectedAndReady);

    Vector3 playerPos = new Vector3(Random.Range(xMin, xMax), 0.0f, Random.Range(zMin, zMax));
    GameObject obj = PhotonNetwork.Instantiate(playerPrefab.name, playerPos, Quaternion.identity);

    cameraObject = GameObject.Find("Camera").GetComponent<Camera>().gameObject;

    cameraObject.transform.parent = obj.transform;

    obj.SetActive(true);
}
Omkar P
  • 29
  • 5

2 Answers2

1

I assume that the scene contains the object with the camera attached. In this case, the problem is that both players share the same object (and the player that created the room is the owner of that object). One approach would be to make a prefab from the player and remove the gameobject from the scene. Then have a PlayerInstantiator that will instantiate the player prefab (use PhotonNetwork.Instantiate), grab the main camera, and attach it to the newly created player. This way all players will have their own gameobject with PlayerMovement attached.

  • thanks for help I have tried the method but no use. Can help me out with how I ca do that. I have shared the updated code above. – Omkar P Jul 25 '22 at 05:45
  • 1
    The problem is still that you're moving both players? If that's the case you can run some tests, for example, the PlayerMovement component can destroy itself (you can check that in Start): `if(!photonView.IsMine) Destroy(this);` Also, you should check that playerPrefab is in the Resources folder. – Marian Ciltea Jul 25 '22 at 23:27
1

First you have to create photon rooms. If you created and joined photon room then you have to create players like codes below

PhotonNetwork.Instantiate("Character/A", transform, rotation);

"Character/A" is your character prefab url. This character must be in the Resource folder and also have Photon Transform View component. I hope it will work for your project.

ghostCommander
  • 366
  • 2
  • 7
  • @gostCommander Thanks for reply I have already separate script that will be responsible for loging in, create and joining room. – Omkar P Jul 25 '22 at 05:43