Players Controlling Each Other

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

Players Controlling Each Other

Samoerai_YT
2022-02-20 22:40:16

Hi guys!

I got a bit of a problem. I’ve been trying to solve it for weeks now but I can’t seem to get it solved unfortunately. I’m very new to unity and C#. So that’s why I came here to ask you guys for some help! : )

So here is the problem:

I have been trying to make my first little fps game a multiplayer game. I have watched a lot of different videos. I ended up using this one: https://youtu.be/93SkbMpWCGo. The only difference is that my game is a 3D game. So I changed Vector2’s into Vector3’s. I will link my project below so you can have a look trough the code.

I did all the steps except the last step which is step 9. Which shouldn't be causing the problem. As long as 1 player joins the game everything is fine. However, as soon as a second player joins the game, they start controlling each other. Also if you press shoot both the players shoot. I have had the same issue using mirror networking. Can anyone please help me with this? Thanks in advance : )

Btw I have been using ParrelSync Clone Manager to test it out.

Here is the link to the project:

GitHub - LightDecipher/LandScape: Just creating my first project and trying to learn Unity and GitHub :)
Just creating my first project and trying to learn Unity and GitHub :) - GitHub - LightDecipher/LandScape: Just creating my first project and trying to learn Unity and GitHub :)

The code:

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

public class PlayerMovement : MonoBehaviour    
{    
  public CharacterController controller;    

  public float speed = 12f;    
  public float gravity = -9.81f;    
  public float jumpHeigt = 3f;    

  public Transform groundCheck;    
  public float groundDistance = 0.4f;    
  public LayerMask groundMask;    

  Vector3 velocity;    
  bool isGrounded;    

  PhotonView view;    

  private void Start()    
  {    
  view = GetComponent<PhotonView>();    
  }    

  // Update is called once per frame    
  void Update()    
  {    
  if (view.IsMine)    
  {    
  isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);    

  if (isGrounded && velocity.y < 0)    
  {    
  velocity.y = -2f;    
  }    

  float x = Input.GetAxis("Horizontal");    
  float z = Input.GetAxis("Vertical");    

  Vector3 move = transform.right * x + transform.forward * z;    

  controller.Move(move * speed * Time.deltaTime);    

  if (Input.GetButtonDown("Jump") && isGrounded)    
  {    
  velocity.y = Mathf.Sqrt(jumpHeigt * -2 * gravity);    
  }    

  velocity.y += gravity * Time.deltaTime;    

  controller.Move(velocity * Time.deltaTime);    
  }    
  else    
  {    
  return;    
  }    
  }    
}    

Comments

Klover
2022-03-28 18:21:11

Just make sure that before you call a function, add the if statement if (PhotonView.IsMine) so that it's only called for that specific player. That's really all there is to it

Samoerai_YT
2022-03-29 11:53:45

Klover 2022-03-28T18:21:11+00:00

Just make sure that before you call a function, add the if statement if (PhotonView.IsMine) so that it's only called for that specific player. That's really all there is to it

Hi! Thanks for the comment :) I tried it and now this happend:

I changed

if (view.IsMine)    

To

if (PhotonView.IsMine)    

like you said.

Now I can’t start the game anymore because all compiler errors need to be fixed. And this is the error:

”error CS0120: An object reference is required for the non-statick field, method, or property ‘PhotonView.IsMine’“.

this wasn’t the case when it was

if (view.IsMine)    

They controlled each other, but at least I didn’t get the error and I was able to startup the game.

Do you (or anyone) know what’s going on here and what I have to do in order for it to work properly?

Any help is highly appreciated!

Thanks in advance :)

Tobias
2022-03-29 14:16:57

Can't really help with this concrete case, sorry. But if nothing else helps, the PUN Basics Tutorial covers use of IsMine.

Samoerai_YT
2022-03-29 20:21:26

Tobias 2022-03-29T14:16:57+00:00

Can't really help with this concrete case, sorry. But if nothing else helps, the PUN Basics Tutorial covers use of IsMine.

I will definitely have a look at that. Thank you!

I will let you guys know if I managed to fix it and how I fixed it so feature people visiting this page can learn from it. I will let you guys know by commenting like I’m doing now. Until then, every help from everyone is always highly appreciated :)

jcjms
2022-07-11 15:45:28

I have exact the same problem. Did you find a solution?

Kind regards :, )

Samoerai_YT
2022-07-23 13:49:19

jcjms 2022-07-11T15:45:28+00:00

I have exact the same problem. Did you find a solution?

Kind regards :, )

Nope 🥲. Also I messed up my complete project. So I had to stop it. I told myself that I would start with the photon PUN tutorial. But im doubting if I should start now, or wait for gpu prices to drop in order for me to build a proper pc. Cuz the MacBook Pro mid 2015 im using struggles a bit as it is quite old at this point

Alejandrazo
2022-07-29 03:34:42

i think the error comes with the variable controller. It looks like not assigned properly in the code posted.

Samoerai_YT
2022-08-02 13:10:31

Alejandrazo 2022-07-29T03:34:42+00:00

i think the error comes with the variable controller. It looks like not assigned properly in the code posted.

Interesting. I’ll have a look at it next time when I get back to it. Thanks :)

AlexXGG1
2023-04-13 09:18:44

I think I solved the problem. I had it too and I noticed ( at least in my case) that you could control the other player because you were the other player, just the camera's switched. So I went and added the PhotonView component to my camera and added this line of code:

(Remember to assign the PhotonView to view in Inspector, otherwise it doesn't work).

This solved my problem, but I don't know if it will work for others.

Back to top