Photon Objects interaction
Hello. I'm using Photon to make a multiplayer VR game. First player is on Oculus and the second is on vive. When I move object by first player (master) its barely ok. It doesn't synchronize when Im holding object, but when i drop it, it's in the correct position for both players. Player who joined to the server can move object like first (no synchronization while moving) but when i drop Object more than half Meter from its parent position its localPosition changes on (0,0,0). This is my Script:
public void grabbedBy (Transform grabber)
{
Debug.Log(grabber.parent.name);
//if (photonView.owner == PhotonNetwork.player)
{
transform.SetParent(grabber);
}
grabbed = true;
_grabber = grabber.gameObject;
GetComponent<Rigidbody>().useGravity = false;
GetComponent<Rigidbody>().isKinematic = true;
}
public void realesedBy(Transform grabber, Vector3 linearVelocity, Vector3 angularVelocity)
{
Rigidbody rigidbody = GetComponent<Rigidbody>();
grabbed = false;
//rigidbody.useGravity = true;
//rigidbody.velocity = linearVelocity;
//rigidbody.angularVelocity = angularVelocity;
//GetComponent<Rigidbody>().useGravity = true;
GetComponent<Rigidbody>().isKinematic = false;
transform.SetParent(GameObject.Find("handler").transform);
_grabber = null;
}
private void OnTriggerEnter(Collider other)
{
//photonView.TransferOwnership(other.gameObject.GetPhotonView().ownerId);
}
private void OnTriggerExit(Collider other)
{
//photonView.TransferOwnership(0);
}
I don't know what should do with that :/
Answer by ChristianSimon · May 08, 2018 at 09:08 AM
Hi,
how is the game object synchronized? Is it synchronized at all? If not, you can attach a PhotonView component to the prefab and network instantiate it at runtime by using PhotonNetwork.Instantiate(...);
or PhotonNetwork.InstantiateSceneObject(...);
. For an easy solution you can also add a PhotonTransformView component and add it to the list of Observed Components of the PhotonView component.
If a player now want to grab the object, you have to check if he is the owner first. If the client is the owner, you can continue. If he is not the owner, you have to request the ownership of the object. If the client is the owner of the object and interacts with it (basically moves it around), it should be properly synchronized on the other client as well.
You can read about Ownership Transfer here.
Hi, I was trying so many combinations of various things, that I've lost. This object isn't instantiate. It's on the scene in specified place. Now it almost works. Objects is owned by Scene if I understand it correctly. When player Enters collider, takes the ownership. When player Exit collider ownership is setting to 0 (it means owner is scene, right?). When object is grabbed its position and rotation are equal grabber. Now it is ok. I don't know why when i set grabber as parent the object jump out from hand. It's now like this:
public void grabbedBy (Transform grabber) { grabbed = true; _grabber = grabber.gameObject; GetComponent().useGravity = false; GetComponent().is$$anonymous$$inematic = true; }
public void realesedBy(Transform grabber, Vector3 linearVelocity, Vector3 angularVelocity) { Rigidbody rigidbody = GetComponent();
grabbed = false;
GetComponent<Rigidbody>().is$$anonymous$$inematic = false;
_grabber = null;
}
private void OnTriggerEnter(Collider other)
{
photonView.TransferOwnership(other.gameObject.GetPhotonView().ownerId);
}
private void OnTriggerExit(Collider other)
{
photonView.TransferOwnership(0);
}
private void OnTriggerStay(Collider other)
{
if (grabbed && _grabber != null)
{
transform.position = _grabber.transform.position;
transform.rotation = _grabber.transform.rotation;
}
}
void OnPhotonSerializeView(PhotonStream stream, Photon$$anonymous$$essageInfo info)
{
if (stream.isWriting)
{
stream.SendNext(transform.position);
stream.SendNext(transform.rotation);
}
else
{
transform.position = (Vector3)stream.ReceiveNext();
transform.rotation = (Quaternion)stream.ReceiveNext();
}
}
Object has PhotonView and PhotonTransformView with Interpolation set up on Estimated speed. Now the problem is that Object is shuttering, while hands of second player move smooth.
I guess the grabbed object has a Rigidbody component, am I right? In this case you would have to make it kinematic and disable its useGravity
property as well, otherwise the client will always run his physics simulation which leads to the described behaviour. You can try adding the OnOwnershipTransfered
callback (see here) and update those two properties accordingly. Firstly check, if this callback is meant to be called on this object. Then, depending on the 'newOwner', enable or disable the properties. If the 'newOwner' is the local client or any other client, the object should be kinematic, if the 'newOwner' is the scene, you should apply gravity, so that the object can fall to the ground.
For the end of this topic, I can add that if both Player has my script Grabber which execute grabbedBy and releasedBy i need to set up gravity of object via RPC to synchronize it. Thanks for help.
Answer by art1997 · May 15, 2020 at 09:27 AM
Hello. I have similar problem. I have cube in scene, with Photon View on it, and with Transform view.
For all user, when they grab that cube, they can move it smoothly. but the movement for other not smooth. The grabbed cube is lagging, jumping.
Do you have idea, how to solve it? Can provide video of it if needed.
Answer by Unknownperson123 · Dec 05, 2021 at 08:48 AM
listen Everyone,
i am making a fps game using photon.
i am running the dev build (two windows for two players). whenever a player jumps and i switch the window, the player who jumps seems to fall a bit and then teleport up and the cycle continues pls help