Search Unity

Third Party Photon - Spawning 3rd person on player bone + Syncing AimIK

Discussion in 'Multiplayer' started by Spectra_7, Oct 6, 2018.

  1. Spectra_7

    Spectra_7

    Joined:
    Oct 17, 2017
    Posts:
    33
    Hello.

    I am creating a FPS multiplayer game using Photon (PUN 1 Classic).
    The problem is I am able to spawn the weapon prefab on the player's right hand bone. It works fine on client's local side, but the other players cannot see what I am holding.

    The TP weapon has photon view and photon transform view added as a observed component, but it is still not working.

    Code for spawning the weapon:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using RootMotion.FinalIK;
    4. using UnityEngine;
    5.  
    6. public class PlayerSetup : Photon.MonoBehaviour {
    7.  
    8.     [SerializeField] private string remotePlayerLayerName = "Remote Player";
    9.     [SerializeField] private string dontDrawLayerName = "Don't Draw";
    10.     [SerializeField] private GameObject playerGraphics;
    11.  
    12.     [SerializeField] private GameObject thirdPersonWeaponPrefab;
    13.     [SerializeField] private Transform lookAtTransform;
    14.     [SerializeField] private Transform aimTargetTransform;
    15.  
    16.  
    17.     private AimIK aimIK;
    18.     private Animator anim;
    19.  
    20.     private Transform rightHandBone;
    21.     private Transform rightHandWeaponHandTransform;
    22.  
    23.     private Vector3 lookAtPosition;
    24.  
    25.     // Use this for initialization
    26.     void Start ()
    27.     {
    28.         if (!photonView.isMine)
    29.             AssignRemoteLayerName();
    30.  
    31.         anim = playerGraphics.GetComponent<Animator>();
    32.         aimIK = playerGraphics.GetComponent<AimIK>();
    33.         rightHandBone = anim.GetBoneTransform(HumanBodyBones.RightHand);
    34.         rightHandWeaponHandTransform = rightHandBone.transform.Find("WeaponHand");
    35.  
    36.         GameObject tpWeapon = PhotonNetwork.Instantiate(thirdPersonWeaponPrefab.name, rightHandWeaponHandTransform.position, rightHandWeaponHandTransform.rotation, 0);
    37.         tpWeapon.transform.parent = rightHandWeaponHandTransform;
    38.         tpWeapon.transform.localScale = thirdPersonWeaponPrefab.transform.localScale;
    39.         aimTargetTransform = tpWeapon.transform.Find("AimTarget");
    40.  
    41. //Hide the body for local player, but remote players can see this.
    42.         SetLayerRecursively(playerGraphics, LayerMask.NameToLayer(dontDrawLayerName));
    43.     }
    44.  
    45.     //Recursive method (DANGEROUS)
    46.     void SetLayerRecursively(GameObject _playerGraphics, int newLayer)
    47.     {
    48.         _playerGraphics.layer = newLayer;
    49.  
    50.         foreach (Transform child in _playerGraphics.transform)
    51.         {
    52.             SetLayerRecursively(child.gameObject, newLayer);
    53.             //child.gameObject.layer = newLayer;
    54.         }
    55.     }
    56.  
    57.     void AssignRemoteLayerName()
    58.     {
    59.         gameObject.layer = LayerMask.NameToLayer(remotePlayerLayerName);
    60.     }
    61.  
    62.     private void LateUpdate()
    63.     {
    64.         aimIK.solver.target = lookAtTransform;
    65.         aimIK.solver.transform = aimTargetTransform;
    66.     }
    67. }
    68.  
    So my questions:
    1. How to sync this third person weapon prefab over the photon network?
    2. Another question, how to sync AimIK over a Photon network? Do I need to add photon view and photon transform view on the bones I added in the AimIK?
     
  2. Spectra_7

    Spectra_7

    Joined:
    Oct 17, 2017
    Posts:
    33
    Bump.

    Anyone please. I am still not able to solve this problem.
     
  3. voncarp

    voncarp

    Joined:
    Jan 3, 2012
    Posts:
    187
    You should call this with a Photon.RPC. The client side is not going to know what to do with the object after it is instantiated. But, it will if you send it as an RPC. After the client receives the RPC, then you can have it do whatever you want. Based on what you have, the RPC should look something like this.

    Code (CSharp):
    1.     void SetupWeapon()
    2.     {
    3.         if (photonView.isMine)
    4.         {
    5.             SetupWeaponReceive();
    6.             photonView.RPC("SetupWeaponReceive", PhotonTargets.Others);
    7.         }  
    8.     }
    9.  
    10.     [PunRPC]
    11.     void SetupWeaponReceive()
    12.     {
    13.         GameObject tpWeapon = Instantiate(thirdPersonWeaponPrefab.name, rightHandWeaponHandTransform.position, rightHandWeaponHandTransform.rotation) as GameObject;
    14.         tpWeapon.transform.parent = rightHandWeaponHandTransform;
    15.         tpWeapon.transform.localScale = thirdPersonWeaponPrefab.transform.localScale;
    16.         //set up your layer code here
    17.     }
     
  4. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,066
    Let us know if you're still stuck and someone from the team can have a look the next days.
     
  5. Spectra_7

    Spectra_7

    Joined:
    Oct 17, 2017
    Posts:
    33
    Hello.
    I did this and it is still not working. I can see the gun spawned for the remote player, but it is not getting attached to bones.
    The remote player also cannot see the gun attached nor the gun spawned somewhere.

    I have added the PhotonView component in that weapon prefab.

    Here is the latest code.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using RootMotion.FinalIK;
    4. using UnityEngine;
    5.  
    6. public class PlayerSetup : Photon.MonoBehaviour {
    7.  
    8.     [SerializeField] private string remotePlayerLayerName = "Remote Player";
    9.     [SerializeField] private string dontDrawLayerName = "Don't Draw";
    10.     [SerializeField] private GameObject playerGraphics;
    11.  
    12.     [SerializeField] private GameObject thirdPersonWeaponPrefab;
    13.     [SerializeField] private Transform lookAtTransform;
    14.     [SerializeField] private Transform aimTargetTransform;
    15.  
    16.  
    17.     private AimIK aimIK;
    18.     private Animator anim;
    19.  
    20.     private Transform rightHandBone;
    21.     private Transform rightHandWeaponHandTransform;
    22.  
    23.     private Vector3 lookAtPosition;
    24.  
    25.     // Use this for initialization
    26.     void Start ()
    27.     {
    28.         if (!photonView.isMine)
    29.             AssignRemoteLayerName();
    30.  
    31.         anim = playerGraphics.GetComponent<Animator>();
    32.         aimIK = playerGraphics.GetComponent<AimIK>();
    33.        
    34.         SetupWeapon();
    35.     }
    36.  
    37.     //Recursive method (DANGEROUS)
    38.     void SetLayerRecursively(GameObject _playerGraphics, int newLayer)
    39.     {
    40.         _playerGraphics.layer = newLayer;
    41.  
    42.         foreach (Transform child in _playerGraphics.transform)
    43.         {
    44.             SetLayerRecursively(child.gameObject, newLayer);
    45.             //child.gameObject.layer = newLayer;
    46.         }
    47.     }
    48.  
    49.     void AssignRemoteLayerName()
    50.     {
    51.         gameObject.layer = LayerMask.NameToLayer(remotePlayerLayerName);
    52.     }
    53.  
    54.     void SetupWeapon()
    55.     {
    56.         if(photonView.isMine)
    57.         {
    58.             SetupWeaponReceive();
    59.             photonView.RPC("SetupWeaponReceive", PhotonTargets.Others);
    60.         }
    61.     }
    62.  
    63.     [PunRPC]
    64.     void SetupWeaponReceive()
    65.     {
    66.         rightHandBone = anim.GetBoneTransform(HumanBodyBones.RightHand);
    67.         rightHandWeaponHandTransform = rightHandBone.transform.Find("WeaponHand");
    68.  
    69.         GameObject tpWeapon = PhotonNetwork.Instantiate(thirdPersonWeaponPrefab.name, rightHandWeaponHandTransform.position, rightHandWeaponHandTransform.rotation, 0) as GameObject;
    70.         tpWeapon.transform.parent = rightHandWeaponHandTransform;
    71.         tpWeapon.transform.localScale = thirdPersonWeaponPrefab.transform.localScale;
    72.  
    73.         aimTargetTransform = tpWeapon.transform.Find("AimTarget");
    74.  
    75.         SetLayerRecursively(playerGraphics, LayerMask.NameToLayer(dontDrawLayerName));
    76.     }
    77.  
    78.     private void LateUpdate()
    79.     {
    80.         aimIK.solver.target = lookAtTransform;
    81.         aimIK.solver.transform = aimTargetTransform;
    82.     }
    83. }
    84.  
     
  6. voncarp

    voncarp

    Joined:
    Jan 3, 2012
    Posts:
    187
    Don't use PhotonNetwork.Instantiate in this case. The RPC is sending it to the clients. All you have to do now is instantiate it locally.

    Code (CSharp):
    1.     [PunRPC]
    2.     void SetupWeaponReceive()
    3.     {
    4.         rightHandBone = anim.GetBoneTransform(HumanBodyBones.RightHand);
    5.         rightHandWeaponHandTransform = rightHandBone.transform.Find("WeaponHand");
    6.  
    7. //Local Instantiate
    8.         GameObject tpWeapon = Instantiate(thirdPersonWeaponPrefab.name, rightHandWeaponHandTransform.position, rightHandWeaponHandTransform.rotation, 0) as GameObject;
    9.         tpWeapon.transform.parent = rightHandWeaponHandTransform;
    10.         tpWeapon.transform.localScale = thirdPersonWeaponPrefab.transform.localScale;
    11.  
    12.         aimTargetTransform = tpWeapon.transform.Find("AimTarget");
    13.  
    14.         SetLayerRecursively(playerGraphics, LayerMask.NameToLayer(dontDrawLayerName));
    15.     }
     
  7. Spectra_7

    Spectra_7

    Joined:
    Oct 17, 2017
    Posts:
    33