Turn Gravity on and off over PUN when someone is interacting with the object

Hello guys =)
In PUN when Objects have a rigidbody with gravity it is not possible to sync the object when someone is interacting with it -> the object is moving on the floor for everyone else not interacting with the object, while the one interacting with it is holding it in his hands in the air.

I tried to solve the problem by turning gravity off the moment the owner of the object switches from scene (ID = 5) to any player (ID !=5).
It works fine when local, but not in the network.
For example:
lifting a ball up in the air and letting go: the player interacting with it sees the ball jumping up in the air the moment he let go and than sees it falling to the ground. The other players see the ball just falling normal to the ground without jumping up in the air the first moment.
When throwing the ball: the player interacting with it sees the ball jumping up again and than it seems like the ball is hitting a transparent wall -> distance until the ball hits the wall depends on the strength of the throw. The others do not see the ball jumping but also see it "hitting" the wall.

When I change "AllViaServer" to "Others" there is no "wall" and when interacting with the object everyone not interacting with it see everything working fine like it should, but the player interacting with it has the object jumping up in the air when letting go or throwing.

I have no idea what is going wrong.
Thanks for any help!!

Here is my script with the RPC turning Gravity on and off depending on the owner of the object.
[RequireComponent(typeof(PhotonView))]
public class PhyManager : Photon.MonoBehaviour
{

    //private bool Interacting;
    private Vector3 speed;
    private int creator;

    void Start()
    {
        creator= this.photonView.ownerId;        
    }

    // Update is called once per frame
    void Update()
    {
        speed = gameObject.GetComponent().velocity;

        if (PhotonNetwork.player.ID == creator)
        {
            photonView.RPC("Gravity", PhotonTargets.AllViaServer, this.photonView.ownerId, speed); 

        }
      
    }
 [PunRPC] 
    void Gravity(int ViewID, Vector3 speedy)
    {
            if (ViewID != 5)
            {
                print("Gravity off");
                gameObject.GetComponent().useGravity = false;
            }

            if (ViewID == 5)
            {
                print("Gravity on");
                gameObject.GetComponent().velocity = speedy;
                gameObject.GetComponent().useGravity = true;
            }
        
    }
}

Comments

  • Hi @deadlysnix,

    is there any other synchronization ongoing, for example is there an attached PhotonTransformView component or similar? If not, please note that the clients don't run the exact same Physics Simulation and therefore results might look different on different clients.

    What you might need is a custom OnPhotonSerializeView solution for this behaviour. Using this you can synchronize the object's position and rotation as well as the Rigidbody's velocity and useGravity properties.
  • @Christian_Simon
    thanks for your answer. I have a photonView and TransformView attached on the objects.
    I managed to have the problem now only on the clients side. when the masterclient is interacting with the object everything is fine. If the client interacts with it, the masterclient sees everything fine, but the client has the "jumping" object. The velocity of the object is constant negativ -> the object is falling even when it is jumping up in the air the fist moment.
    I don't know how to solve this.
  • In order to let another client handle the object, you have to transfer the Ownership to this client first. You currently have the following situation: if the Owner of the object handles the object, he will also synchronize the object across the network. This is the correct behaviour. If another client (not the Owner) handles the object, he will update it locally and it gets 'reset' afterwards, when the Owner synchronizes the object again. To avoid this you can transfer the Ownership to the client who wants to handle the object. To see how this Ownership Transfer works, you can take a look at the related demo from the PUN package and / or take a look at the related documentation page.
  • edited July 2018
    @Christian_Simon
    I have an change owner script which transfers the owner to the player interacting with an object and after the interaction ends back to the scene(5)
    
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    
    [RequireComponent(typeof(PhotonView))]
    public class ChangeOwner : Photon.PunBehaviour {
    
        private ControllerSkript attachedWand;
    
        // Use this for initialization
        void Start () {
    
            if (this.photonView.ownerId == PhotonNetwork.player.ID)
            {
                photonView.RPC("Uebergabe2", PhotonTargets.AllViaServer, "Ende");
            }
        }
        public void InteraktionGriffAnfang(ControllerSkript wand)
        {
            print("start Interaction, Current Owner: " + this.photonView.ownerId);
            attachedWand = wand;
            if (this.photonView.ownerId == 5)
            {
                this.photonView.TransferOwnership(PhotonNetwork.player.ID);
                if(this.photonView.ownerId == PhotonNetwork.player.ID)
                photonView.RPC("Uebergabe2", PhotonTargets.AllViaServer, "Anfang");
            }
    
            else
            {
                Debug.Log("Object is already used by player" + this.photonView.ownerId);
                return;
            }
        }
        public void InteraktionGriffEnde(ControllerSkript wand)
        {
            print("ende interaktion");
            if (wand == attachedWand)
            {
                attachedWand = null;
                photonView.RPC("Uebergabe2", PhotonTargets.AllViaServer, "Ende");
                this.photonView.TransferOwnership(5);
            }
        }
        [PunRPC]
        void Uebergabe2(string a)
        {
            if (a == "Anfang") 
            {
                if(this.photonView.ownerId == PhotonNetwork.player.ID)
                this.photonView.TransferOwnership(PhotonNetwork.player.ID);
            }
    
            if (a == "Ende")
            {
                this.photonView.TransferOwnership(5);
            } 
        }
    }
  • Guys I have a similar problem with two VR users. When the master user manipulates a cube everything is fine but in the view on the second player it looks like the cube is fighting against gravity. If I disable gravity everything is ok? Any idea?
Sign In or Register to comment.