Home > Blockchain >  Photon player moment issue
Photon player moment issue

Time:07-23

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.

CodePudding user response:

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.

CodePudding user response:

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.

  • Related