- Home /
Question by
unity_Ghh8LYElB8EDbw · Sep 20, 2021 at 04:22 AM ·
c#photonmultiplayer-networking
Unity Photon Transform movement applying to opposite prefab
I have set up a photon server using this tutorial, but when the second player joins the game the second player's input is applied to the first player and the first player's input is applied to the second player.
Here is my mouseLook script: (my movement script is very long and they both have the same issue so I posted this one)
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
using UnityEngine.UI;
using Photon.Pun;
public class mouseLook : MonoBehaviour
{
public float mouseSensitivity = 100f;
public Transform playerBody;
public Slider sensitivity; // sensitivity.value
public bool gameIsLocal;
PhotonView view;
float xRotation = 0f;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
if (!gameIsLocal)
{
view = GetComponent<PhotonView>();
}
else
{
return;
}
try // Load mouse settings
{
LoadMouseSettings();
}
catch
{
return;
}
}
void Update()
{
//Check if it's singleplayer
if (gameIsLocal)
{
Look();
Debug.Log("Game is Local");
}
//Check if it's ur player character
else if (view.IsMine && !gameIsLocal)
{
Look();
}
else
{
return;
}
}
void Look()
{
float mouseX = Input.GetAxis("Mouse X") * sensitivity.value * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * sensitivity.value * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(xRotation, 0, 0);
playerBody.Rotate(Vector3.up * mouseX);
}
void LoadMouseSettings()
{
sensitivity.value = PlayerPrefs.GetFloat("Sensitivity");
}
}
Here is the Player Prefab:
helpme.png
(76.5 kB)
Comment
Answer by unity_Ghh8LYElB8EDbw · Sep 20, 2021 at 10:57 PM
Credit to Malace from Photon Forums.
Instead of checking for 'view.isMine' in Update, just delete the other instance of the script when Awake is called.
private void Awake()
{
if (!photonView.IsMine && GetComponent<PlayerScript>() != null)
{
Debug.Log("Destroying Player script");
Destroy(GetComponent<PlayerScript>());
}
}
Additionally, make sure that the cameras on the Player prefab are disabled and should only be enabled once the player spawns. Or else Photon will mirror the movement of the players.