Players Controlling Each Other
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:
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; } } }
Answers
-
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
0 -
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 :)
0 -
Can't really help with this concrete case, sorry. But if nothing else helps, the PUN Basics Tutorial covers use of IsMine.
0 -
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 :)
0 -
I have exact the same problem. Did you find a solution?
Kind regards :, )
0 -
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
0 -
i think the error comes with the variable controller. It looks like not assigned properly in the code posted.
0 -
Interesting. I’ll have a look at it next time when I get back to it. Thanks :)
0 -
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.
1