Search Unity

  1. The 2022.1 beta is now available for testing. To find out what's new, have a look at our 2022.1 beta blog post.
    Dismiss Notice

pick up an item

Discussion in 'Multiplayer' started by christianlebedev1, Jan 7, 2022.

  1. christianlebedev1

    christianlebedev1

    Joined:
    Dec 5, 2020
    Posts:
    14
    Hello,
    The second player does not see how the first picks up the object. How to fix it?
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Animations.Rigging;
    5. using UnityEngine.UI;
    6. using Photon.Pun;
    7. using Photon.Realtime;
    8.  
    9. public class WeaponPick : MonoBehaviourPun, IPunObservable
    10. {
    11.     public Transform equipPosition;
    12.     public float distance = 10f;
    13.     GameObject currentWeapon;
    14.     GameObject wp;
    15.     Vector3 realPosition = Vector3.zero;
    16.     Quaternion realRotation = Quaternion.identity;
    17.  
    18.  
    19.  
    20.     public GameObject camera;
    21.     public Transform cameraa;
    22.     private int targetValue;
    23.     PhotonView PV;
    24.     bool canGrab;
    25.    
    26.     void Awake()
    27.     {
    28.         PV = GetComponent<PhotonView>();
    29.     }
    30.     void Start()
    31.     {
    32.   rig = GameObject.Find("CameraRig").GetComponent<Rig>();
    33.         equipPosition = GameObject.Find("Destination").GetComponent<Transform>();
    34.     }
    35.  
    36.     public void Update()
    37.     {
    38.         if (PV.IsMine)
    39.         {
    40.         realPosition = transform.position;
    41.             CheckWeapons();
    42.  
    43.             if (canGrab)
    44.             {
    45.              
    46.                 if (Input.GetKeyDown(KeyCode.E))
    47.                 {
    48.                     if (currentWeapon != null)
    49.                         Drop();
    50.  
    51.                     PickUp();
    52.  
    53.                 }
    54.             }
    55.  
    56.             if (currentWeapon != null)
    57.             {
    58.            
    59.                 {
    60.                     Drop();
    61.  
    62.                 }
    63.  
    64.             }
    65.         }
    66.     }
    67.  
    68.     public void CheckWeapons()
    69.     {
    70.      
    71.  
    72.         RaycastHit hit;
    73.  
    74.             if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, distance))
    75.             {
    76.                 if (hit.transform.tag == "CanGrab")
    77.                 {
    78.                     canGrab = true;
    79.                     wp = hit.transform.gameObject;
    80.                 }
    81.             }
    82.             else
    83.                 canGrab = false;  
    84.     }
    85.     public void PickUp()
    86.     {
    87.            camera.gameObject.tag = "Cameraoff";
    88.             currentWeapon = wp;
    89.             currentWeapon.transform.position = equipPosition.position;
    90.             currentWeapon.transform.parent = equipPosition;
    91.             currentWeapon.transform.localEulerAngles = new Vector3(0f, 0f, 0f);
    92.             currentWeapon.GetComponent<Rigidbody>().isKinematic = true;
    93.             currentWeapon.GetComponent<BoxCollider>().isTrigger = true;
    94.        
    95.     }
    96.  
    97.     public void Drop()
    98.     {
    99.             camera.gameObject.tag = "CanGrab";
    100.             currentWeapon.transform.parent = null;
    101.             currentWeapon.GetComponent<BoxCollider>().isTrigger = false;
    102.             currentWeapon.GetComponent<Rigidbody>().isKinematic = false;
    103.             currentWeapon = null;
    104.        
    105.     }
    106.  
    107.     public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    108.     {
    109.         if (stream.IsWriting)
    110.         {
    111.  
    112.             stream.SendNext(transform.position);
    113.             stream.SendNext(transform.rotation);
    114.         }
    115.         else
    116.         {
    117.             realPosition = (Vector3)stream.ReceiveNext();
    118.             realRotation = (Quaternion)stream.ReceiveNext();          
    119.         }
    120.     }
    121. }
     
  2. christianlebedev1

    christianlebedev1

    Joined:
    Dec 5, 2020
    Posts:
    14
    Found a way out by watching this
     
unityunity