Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Heffalus · Mar 10, 2015 at 02:23 PM · c#photonglobal variablesstatic-variables

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);        
         }
     }
 }

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Pendantic · Mar 10, 2015 at 02:28 PM 0
Share

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.

avatar image Heffalus · Mar 10, 2015 at 02:55 PM 0
Share

ok i added the scripts for movement.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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>();
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Photon instantiate and addforce to arrow 0 Answers

Renderer on object disabled after level reload 1 Answer

how to add private chat in multuplayer game. 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges