Gameobject is jittering when its ownership is transferred to another user!
The whole answer can be found below.
Try Our
Documentation
Please check if you can find an answer in our extensive documentation on PUN.
Join Us
on Discord
Meet and talk to our staff and the entire Photon-Community via Discord.
Read More on
Stack Overflow
Find more information on Stack Overflow (for Circle members only).
Gameobject is jittering when its ownership is transferred to another user!
DanieleSuppo
2022-12-01 16:54:22
Hello, I have a very big problem: I have a Gameobject that can be moved by 2 users, and basically each of them take its ownership pressing a button. Unfortunately, after some movements (just position and rotation), when one of them take its ownership it "jitter" quite a lot (it move to another position and back to the right one) on the user that "lost" the ownership (but not on the user that is the new owner).
This issue does not happen just in my game, but in the official PUN tutorial too!
It's quite easy to reproduce:
1 - create a Cube, with its PhotonView and PhotonTransformView
2 - create a simple script, attached to the cube, with the function "pv.RequestOwnership();" and a button that call this function
3 - in gameplay, user1 can move and rotate the cube with the mouse in the scene, and its position/rotation are updated on user2
4 - user2 press the button to take the ownership, and to move and rotate the cube, and its position/rotation are updated on user1
5 - after a couple of movements, and changing from user1 and user2, you will see that the cube, on the user that just lost the ownership, will move around its position a bit
Is it a (huge) bug?
Many thanks!
Comments
If a user just passed control to the other, this other player didn't send an update yet but the interpolation is still trying to smoothly move the cube from a to b. I guess this could cause jitter.
The PhotonTransformView is considered a basis implementation for you to expand on. You may want to replace it with a customized version that prevents interpolation when ownership was handed over. If this is a physical object, make sure IsKinematic is applied, so the physics engine is not moving the object, too.
Controlling one object with multiple users is a bit beyond PUN's capabilities. I would recommend using Fusion for this kind of interaction (and run the simulation on a Host/Server). It's been used to toss objects from user to user in VR.
I have this problem too with PUN2. I already set isKinematic, but the object jitter first to the old position and after that there is no problem anymore. It only "jitter" for the other ones, if the ownership gets transfered again.
So how could i prevent the interpolation when the ownership is handed over? Any ideas?
Thanks in Advance ;)
EDIT: Okay finally i got it to work. In my Example im just syncing an EulerAngle Y (m_eulerAngleY). Maybe there is a better Solution for this, but i added IPunOwnershipCallbacks in Custom Version from the TransformView and added this:
public void OnOwnershipTransfered(PhotonView targetView, Player previousOwner)
{
if (previousOwner.ActorNumber == PhotonNetwork.LocalPlayer.ActorNumber)
{
isFirstValue = true;
}
}
In my Update Function i will set the Angle manually and afterwards i start the Interpolation.
//If view is not Mine && valuesrecieve
d
if (isFirstValue)
{
m_eulerAngleY = tf.localRotation.eulerAngles.y;
tf.localRotation = Quaternion.Euler(0, m_eulerAngleY, 0);
isFirstValue = false;
}
else
tf.localRotation = Quaternion.Lerp(tf.localRotation, Quaternion.Euler(0, m_eulerAngleY, 0), 0.03f);
Maybe i can get a Feedback? ^^
DanieleSuppo
2022-12-09 13:09:49
I've solved enabling and disabling the PhotonTransformView component:
public
- PhotonView pv;
public bool
- imMovingTheGameobject;
List
List
(); void Start()
{
foreach (var observed in
pv.ObservedComponents)
{
if (observed.gameObject.GetComponent
)
{
PhotonTransformView phv = observed.gameObject.GetComponent
();
ptvList.Add
(phv);
}
}
}
void
Update()
{
if
(!pv.IsMine)
{
SetTransformViewEnabled(true
);
}
else
{
SetTransformViewEnabled(imMovingTheGameobject);
}
}
void SetTransformViewEnabled(bool value
)
{
foreach (var ptv in
ptvList)
{
if (ptv.enabled != value)
ptv.enabled = value
;
}
}