0

I am relatively new to C# and PUN but I felt like giving it a shot. Unfortunately, I am currently having an error that I can't seem to fix no matter what I do! I am making a 3D top down game where the player looks at the mouse and rotates according to where it is. The issue I am having here is that if one player moves his mouse around, the other player will also move around just with a small delay.

This is my Mouse Controller Script. It is attached to an empty game object which is a child of my player prefab. In order to get the camera I have attached it as a child of my camera which is also a child of my player prefab. I tried putting it directly on the player prefab but I was getting the same issue.

I was able to have both players move properly and the cameras properly synced to each other but I can't figure out why it doesn't work for this one!

Thanks for your time!

PS: this is my first time posting so if I did something wrong or I am missing information feel free to let me know :)

C#

 [SerializeField] private LayerMask groundMask;
    public bool mouseOnGround;
    public Vector3 mousePosition;
    public Vector3 aimDirection;
    public Camera playerCam;
    public float myMouseInput;
    public PhotonView PV;
    // Start is called before the first frame update
    void Start()
    {
        PV = GetComponent<PhotonView>();


        ///THIS HAS NOT WORKED AND SIMPLY PREVENTS PLAYERS FROM ROTATING   
        if (!photonView.IsMine)
        {
            {

                Destroy(this);

            }
        }


        if ((photonView.IsMine))
        {
            playerCam = GetComponentInParent<Camera>();
                             
        }

       
    }

    // Update is called once per frame
    void Update()
    {
      

        if(this.photonView.IsMine)
        {
            if (mouseOnGround == true)
            {
                MouseAim();
                GetMousePosition();
            }

        }
    }

    public void MouseAim()
    {

        if (this.photonView.IsMine)
        {

            if (mouseOnGround == true)
            {

                aimDirection = mousePosition - transform.position;
                aimDirection.y = 0;
                transform.forward = aimDirection;

            }

        }
    }


    public void GetMousePosition()
    {
        if (this.photonView.IsMine)
        {




            var Ray = playerCam.ScreenPointToRay(Input.mousePosition);

            if (Physics.Raycast(Ray, out var hitInfo, Mathf.Infinity, groundMask))
            {

                mouseOnGround = true;
                mousePosition = hitInfo.point;
                Debug.Log(mousePosition);

            }
            else
            {

                mouseOnGround = false;
                mousePosition = Vector3.zero;

            }


        }




    }

I tried including the functions in my player controller instead of having a separate script since the player movements and camera options are in there and they seem to sync properly over the network.

I tried doing different checks for photonView.IsMine in Awake(), Start() and Update()

I tried attaching the script directly on the Player prefab which has the Photon View component, without luck.

1 Answers1

0

I think it is not about this script, this script should work. I think it is about the camera, probably both of the players are looking at the same camera. Why? Because if you don't destroy the camera that is not yours, then all the players will see the game view in the same camera. You should add this script.

public Camera cam; if(!this.photonView.isMine){Destroy(cam);}