- Home /
PhotonNetwork problem
Ive just added Photon Unity Networking to my game by following the Multiplayer FPS tutorial, and im succesfully connecting with multiple clients. The problem is only the last player to join the room can actually do anything. move, shoot and so on.
I think the problem may lie in that i am using global class instance variables to access the different scripts the player uses.
public static <class> Instance;
Instance = this;
i tried researching it but couldnt find any answers, could this be causing the issues?
EDIT:
Controller class
using UnityEngine;
using System.Collections;
public class tpController : Photon.MonoBehaviour {
public static CharacterController CharacterController;
public static tpController Instance;
public bool FreeLook = false;
public float LedgePos;
public bool climbable = false;
public GameObject inventory;
public bool showInventory = false;
GameObject gameMenu;
public bool showGameMenu = false;
tpMotor motor;
void Awake () {
if (!photonView.isMine) {
this.enabled = false;
}
motor = GetComponent<tpMotor> ();
CharacterController = GetComponent ("CharacterController") as CharacterController;
Instance = this;
tpCamera.UseExistingOrCreateNewMainCamera ();
inventory = GameObject.Instantiate (inventory, Vector3.zero, new Quaternion (0, 0, 0, 0)) as GameObject;
inventory.SetActive (false);
Cursor.visible = false;
gameMenu = GameObject.Find ("InGameMenu");
gameMenu.SetActive (false);
}
void Update () {
if (Camera.main == null) {
return;
}
tpMotor.Instance.ResetMotor ();
OpenCloseInventory ();
OpenMainMenu ();
if (!tpAnimator.Instance.IsDead &&
tpAnimator.Instance.State != tpAnimator.CharacterState.Using &&
tpAnimator.Instance.State != tpAnimator.CharacterState.Attacking &&
!showInventory &&
!showGameMenu &&
(tpAnimator.Instance.State != tpAnimator.CharacterState.Landing ||
tpAnimator.Instance.GetComponent<Animation>().IsPlaying(tpAnimator.Instance.runLandingAnimation)) &&
tpAnimator.Instance.State != tpAnimator.CharacterState.Climbing) {
RaycastHit hits;
if (Physics.Raycast(transform.position, Vector3.up, out hits)) {
if(hits.collider.tag == "Ledge") {
LedgePos = hits.distance;
if (LedgePos <= 10f)
climbable = true;
else
climbable = false;
} else {
climbable = false;
}
}
GetLocomotionInput ();
//PlayerAttack.Instance.CheckPlayerAttackInput ();
HandleActionInput ();
} else if (tpAnimator.Instance.IsDead) {
if (Input.anyKeyDown) {
tpAnimator.Instance.Reset();
}
}
tpMotor.Instance.UpdateMotor ();
//Test ();
}
void Test () {
RaycastHit hit;
if (Physics.Raycast(transform.position, Vector3.down, out hit)) {
transform.position = hit.point;
}
}
void HandleActionInput () {
if (Input.GetButton ("Jump")) {
if (climbable) {
Climb();
} else {
Jump ();
}
}
if (Input.GetKeyDown (KeyCode.E)) {
Use();
}
if (Input.GetKeyDown(KeyCode.Q)) {
SwitchWeapon ();
}
if (Input.GetMouseButtonUp (1)) {
if (showInventory || showGameMenu)
return;
if (EquipmentScript.Instance.EquippedWeaponSlot != null) {
if (EquipmentScript.Instance.EquippedWeaponSlot.GetComponent<DroppedItem>().item.itemType == Item.ItemType.RangedWeapon) {
Shoot();
} else if (EquipmentScript.Instance.isShieldInHand) {
// block
Block();
}
}
}
if (Input.GetKeyDown (KeyCode.LeftAlt)) {
FreeLook = true;
}
if (Input.GetKeyUp (KeyCode.LeftAlt)) {
FreeLook = false;
}
if (Input.GetKeyDown (KeyCode.LeftControl)) {
Crouch();
}
}
void OpenCloseInventory() {
if (Input.GetKeyDown(KeyCode.I)) {
showInventory = !showInventory;
Cursor.visible = showInventory;
inventory.SetActive(showInventory);
Debug.Log(inventory.GetActive());
}
}
void OpenMainMenu () {
if (Input.GetKeyDown (KeyCode.Escape)) {
showGameMenu = !showGameMenu;
gameMenu.SetActive(showGameMenu);
gameMenu.GetComponent<GameMenu>().ShowMenu(gameMenu.transform.FindChild("GameMenuPanel").GetComponent<Menu>());
Cursor.visible = showGameMenu;
}
}
public void Jump () {
if (tpAnimator.Instance.State != tpAnimator.CharacterState.Jumping) {
tpMotor.Instance.Jump ();
tpAnimator.Instance.Jump ();
PlayerFootsteps.Instance.JumpSound();
}
}
public void Crouch() {
tpAnimator.Instance.Crouch ();
}
public void SwitchWeapon () {
//StartCoroutine (tpAnimator.Instance.SwitchWeapons ());
//tpAnimator.Instance.SwitchWeapons ();
EquipmentScript.Instance.SwitchWeaponSlots ();
}
public void Use () {
tpAnimator.Instance.Use();
}
public void Die () {
tpAnimator.Instance.Die ();
}
public void Attack () {
tpAnimator.Instance.Attack ();
}
public void Shoot () {
if (tpAnimator.Instance.isShooting) {
tpAnimator.Instance.LooseArrow();
return;
}
tpAnimator.Instance.DrawBow ();
}
public void Block () {
tpAnimator.Instance.Block ();
}
public void Climb ( ) {
tpAnimator.Instance.Climb ();
}
void GetLocomotionInput() {
var deadZone = 0.1f;
if (Input.GetAxis ("Vertical") > deadZone || Input.GetAxis ("Vertical") < -deadZone) {
tpMotor.Instance.MoveVector += new Vector3(0,0,Input.GetAxis("Vertical"));
}
if (Input.GetAxis ("Horizontal") > deadZone || Input.GetAxis ("Horizontal") < -deadZone) {
tpMotor.Instance.MoveVector += new Vector3(Input.GetAxis("Horizontal"),0,0);
}
tpAnimator.Instance.DetermineCurrentMoveDirection ();
}
}
Motor class
using UnityEngine;
using System.Collections;
public class tpMotor : Photon.MonoBehaviour {
public static tpMotor Instance;
public float ForwardSpeed = 10f;
public float RunSpeed = 20f;
public float CrouchSpeed = 1f;
public float BackwardSpeed = 2f;
public float StrafingSpeed = 5f;
public float SlideSpeed = 10f;
public float JumpSpeed = 6f;
public float Gravity = 21f;
public float TerminalVelocity = 20f;
public float SlideThreshold = 0.6f;
public float MaxControllableSlideMagnitude = 0.4f;
public float MaxUndamagingFallHeight = 7f;
public Vector3 MoveVector { get; set; }
public float VerticalVelocity { get; set; }
public bool IsSliding { get; set; }
private Vector3 slideDirection;
private float startFallHeight;
private bool isFalling = false;
public bool IsFalling {
get{ return isFalling;}
set
{
isFalling = value;
if (isFalling)
{
startFallHeight = transform.position.y;
}
else {
if (VerticalVelocity < -20) {
PlayerHealth.Instance.TakeDamage(VerticalVelocity - VerticalVelocity * 2);
PlayerFootsteps.Instance.TakeFallDamage();
startFallHeight = 0;
}
}
}
}
void Awake () {
if (!photonView.isMine) {
this.enabled = false;
}
Instance = this;
}
public void UpdateMotor () {
SnapAlignCharacterWithCamera ();
ProcessMotion ();
}
public void ResetMotor () {
VerticalVelocity = MoveVector.y;
MoveVector = Vector3.zero;
}
void ProcessMotion () {
//Transform MoveVector to WOrld SPace
if (!tpAnimator.Instance.IsDead)
MoveVector = transform.TransformDirection (MoveVector);
else
MoveVector = new Vector3(0, MoveVector.y, 0);
//Normalize MoveVector if magnitude > 0
if (MoveVector.magnitude > 1) {
MoveVector = Vector3.Normalize (MoveVector);
}
// apply sliding if applicable
ApplySlide ();
//Multiply normalized MoveVector by MoveSpeed
MoveVector *= MoveSpeed();
// reapply vertivalvelocity to movevector.y
MoveVector = new Vector3 (MoveVector.x, VerticalVelocity, MoveVector.z);
//apply gravity
ApplyGravity ();
if (tpController.CharacterController.isGrounded) {
MoveVector = new Vector3(MoveVector.x, VerticalVelocity + -1, MoveVector.z);
}
//Move Character in world Space and get units/seconds
tpController.CharacterController.Move (MoveVector * Time.deltaTime);
}
float MoveSpeed () {
var moveSpeed = 0f;
switch (tpAnimator.Instance.MoveDirection) {
case tpAnimator.Direction.Stationary:
moveSpeed = 0f;
break;
case tpAnimator.Direction.Forward:
if (tpAnimator.Instance.isRunning != false) {
moveSpeed = RunSpeed;
} else {
moveSpeed = ForwardSpeed;
}
break;
case tpAnimator.Direction.Backward:
moveSpeed = BackwardSpeed;
break;
case tpAnimator.Direction.StrafeLeft:
moveSpeed = StrafingSpeed;
break;
case tpAnimator.Direction.StrafeRight:
moveSpeed = StrafingSpeed;
break;
case tpAnimator.Direction.LeftForward:
if (tpAnimator.Instance.isRunning != false) {
moveSpeed = RunSpeed;
} else {
moveSpeed = ForwardSpeed;
}
break;
case tpAnimator.Direction.RightForward:
if (tpAnimator.Instance.isRunning != false) {
moveSpeed = RunSpeed;
} else {
moveSpeed = ForwardSpeed;
}
break;
case tpAnimator.Direction.LeftBackward:
moveSpeed = BackwardSpeed;
break;
case tpAnimator.Direction.RightBackward:
moveSpeed = BackwardSpeed;
break;
}
if (IsSliding)
moveSpeed = SlideSpeed;
return moveSpeed;
}
void ApplyGravity () {
if (MoveVector.y > -TerminalVelocity) {
MoveVector = new Vector3(MoveVector.x, MoveVector.y - Gravity * Time.deltaTime, MoveVector.z);
}
if (tpController.CharacterController.isGrounded && MoveVector.y < -1) {
MoveVector = new Vector3(MoveVector.x, -1, MoveVector.z);
}
}
void ApplySlide () {
if (!tpController.CharacterController.isGrounded) {
return;
}
slideDirection = Vector3.zero;
RaycastHit hitInfo;
if (Physics.Raycast(transform.position/** + Vector3.up**/, Vector3.down, out hitInfo)) {
if (hitInfo.normal.y < SlideThreshold) {
slideDirection = new Vector3(hitInfo.normal.x, -hitInfo.normal.y, hitInfo.normal.z);
if (!IsSliding)
{
tpAnimator.Instance.Slide();
}
IsSliding = true;
}
else {
IsSliding = false;
}
}
if (slideDirection.magnitude < MaxControllableSlideMagnitude) {
MoveVector += slideDirection;
} else {
MoveVector = slideDirection;
}
}
public void Jump () {
if (tpController.CharacterController.isGrounded) {
VerticalVelocity = JumpSpeed;
}
}
void SnapAlignCharacterWithCamera () {
if (!tpController.Instance.FreeLook) {
transform.rotation = Quaternion.Euler(transform.eulerAngles.x,
Camera.main.transform.eulerAngles.y,
transform.eulerAngles.z);
}
}
}
show us how your movement code works because photon is natively a client side controlled environment its unlikely static variables are changing unless you are observing them and changing them in your code.
Answer by Heffalus · Mar 11, 2015 at 03:20 PM
As i suspected it was the script referencing that caused the problem, i changed it and now it works :)
public tpController controller;
controller = GetComponent<tpController>();
Your answer
![](https://koobas.hobune.stream/wayback/20220613185630im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Photon instantiate and addforce to arrow 0 Answers